Spaces:
Running
Running
Upload 15 files
Browse files- app/.pyfun +334 -0
- app/README.md +2 -0
- app/__init__.py +1 -0
- app/app.py +213 -0
- app/config.py +293 -0
- app/db_sync.py +302 -0
- app/mcp.py +289 -0
- app/mcp_old.py +379 -0
- app/models.py +73 -0
- app/provider.py +210 -0
- app/tmp/__init__.py +1 -0
- app/tools.py +228 -0
- app/web/tmp/__init__py +1 -0
- app/web/tools/README.md +1 -0
- app/web/tools/__init__.py +1 -0
app/.pyfun
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# .pyfun — PyFundaments App Configuration
|
| 3 |
+
# Single source of truth for app/* modules (provider, models, tools, hub)
|
| 4 |
+
# Part of: Universal MCP Hub on PyFundaments
|
| 5 |
+
# =============================================================================
|
| 6 |
+
# RULES:
|
| 7 |
+
# - All values in double quotes "value"
|
| 8 |
+
# - NO secrets here! Keys stay in .env → only ENV-VAR NAMES referenced here
|
| 9 |
+
# - Comment-out unused sections with # → keep structure, parsers need it!
|
| 10 |
+
# - DO NOT DELETE headers or [X_END] → parsers rely on these markers
|
| 11 |
+
# - Empty/unused values: "" → never leave bare =
|
| 12 |
+
# =============================================================================
|
| 13 |
+
# TIERS:
|
| 14 |
+
# LAZY: fill [HUB] + one [LLM_PROVIDER.*] only → works
|
| 15 |
+
# NORMAL: + [SEARCH_PROVIDER.*] + [MODELS.*] → works better
|
| 16 |
+
# PRODUCTIVE: + [TOOLS] + [FALLBACK] + [HUB_LIMITS] → full power
|
| 17 |
+
# =============================================================================
|
| 18 |
+
# DO NOT DELETE — file identifier used by all parsers
|
| 19 |
+
[PYFUN_FILE = .pyfun]
|
| 20 |
+
|
| 21 |
+
# =============================================================================
|
| 22 |
+
# HUB — Core identity & transport config
|
| 23 |
+
# =============================================================================
|
| 24 |
+
[HUB]
|
| 25 |
+
HUB_NAME = "Universal MCP Hub"
|
| 26 |
+
HUB_VERSION = "1.0.0"
|
| 27 |
+
HUB_DESCRIPTION = "Universal MCP Hub built on PyFundaments"
|
| 28 |
+
|
| 29 |
+
# Transport: stdio (local/Claude Desktop) | sse (HuggingFace/Remote)
|
| 30 |
+
# Override via ENV: MCP_TRANSPORT
|
| 31 |
+
HUB_TRANSPORT = "sse"
|
| 32 |
+
HUB_HOST = "0.0.0.0"
|
| 33 |
+
HUB_PORT = "7860"
|
| 34 |
+
|
| 35 |
+
# App mode: mcp | app
|
| 36 |
+
# Override via ENV: APP_MODE
|
| 37 |
+
HUB_MODE = "mcp"
|
| 38 |
+
|
| 39 |
+
# HuggingFace Space URL (used as HTTP-Referer for some APIs)
|
| 40 |
+
HUB_SPACE_URL = ""
|
| 41 |
+
[HUB_END]
|
| 42 |
+
|
| 43 |
+
# =============================================================================
|
| 44 |
+
# HUB_LIMITS — Request & retry behavior
|
| 45 |
+
# =============================================================================
|
| 46 |
+
[HUB_LIMITS]
|
| 47 |
+
MAX_PARALLEL_REQUESTS = "5"
|
| 48 |
+
RETRY_COUNT = "3"
|
| 49 |
+
RETRY_DELAY_SEC = "2"
|
| 50 |
+
REQUEST_TIMEOUT_SEC = "60"
|
| 51 |
+
SEARCH_TIMEOUT_SEC = "30"
|
| 52 |
+
[HUB_LIMITS_END]
|
| 53 |
+
|
| 54 |
+
# =============================================================================
|
| 55 |
+
# PROVIDERS — All external API providers
|
| 56 |
+
# Secrets stay in .env! Only ENV-VAR NAMES are referenced here.
|
| 57 |
+
# =============================================================================
|
| 58 |
+
[PROVIDERS]
|
| 59 |
+
|
| 60 |
+
# ── LLM Providers ─────────────────────────────────────────────────────────────
|
| 61 |
+
[LLM_PROVIDERS]
|
| 62 |
+
|
| 63 |
+
[LLM_PROVIDER.anthropic]
|
| 64 |
+
active = "true"
|
| 65 |
+
base_url = "https://api.anthropic.com/v1"
|
| 66 |
+
env_key = "ANTHROPIC_API_KEY" # → .env: ANTHROPIC_API_KEY=sk-ant-...
|
| 67 |
+
api_version_header = "2023-06-01" # anthropic-version header
|
| 68 |
+
default_model = "claude-haiku-4-5-20251001"
|
| 69 |
+
models = "claude-opus-4-6, claude-sonnet-4-6, claude-haiku-4-5-20251001"
|
| 70 |
+
fallback_to = "openrouter" # if this provider fails → try next
|
| 71 |
+
[LLM_PROVIDER.anthropic_END]
|
| 72 |
+
|
| 73 |
+
[LLM_PROVIDER.gemini]
|
| 74 |
+
active = "true"
|
| 75 |
+
base_url = "https://generativelanguage.googleapis.com/v1beta"
|
| 76 |
+
env_key = "GEMINI_API_KEY" # → .env: GEMINI_API_KEY=...
|
| 77 |
+
default_model = "gemini-2.0-flash"
|
| 78 |
+
models = "gemini-2.0-flash, gemini-1.5-pro, gemini-1.5-flash"
|
| 79 |
+
fallback_to = "openrouter"
|
| 80 |
+
[LLM_PROVIDER.gemini_END]
|
| 81 |
+
|
| 82 |
+
[LLM_PROVIDER.openrouter]
|
| 83 |
+
active = "true"
|
| 84 |
+
base_url = "https://openrouter.ai/api/v1"
|
| 85 |
+
env_key = "OPENROUTER_API_KEY" # → .env: OPENROUTER_API_KEY=sk-or-...
|
| 86 |
+
default_model = "mistralai/mistral-7b-instruct"
|
| 87 |
+
models = "openai/gpt-4o, meta-llama/llama-3-8b-instruct, mistralai/mistral-7b-instruct"
|
| 88 |
+
fallback_to = "" # last in chain, no further fallback
|
| 89 |
+
[LLM_PROVIDER.openrouter_END]
|
| 90 |
+
|
| 91 |
+
[LLM_PROVIDER.huggingface]
|
| 92 |
+
active = "true"
|
| 93 |
+
base_url = "https://api-inference.huggingface.co/models"
|
| 94 |
+
env_key = "HF_TOKEN" # → .env: HF_TOKEN=hf_...
|
| 95 |
+
default_model = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 96 |
+
models = "mistralai/Mistral-7B-Instruct-v0.3, meta-llama/Llama-3.3-70B-Instruct"
|
| 97 |
+
fallback_to = ""
|
| 98 |
+
[LLM_PROVIDER.huggingface_END]
|
| 99 |
+
|
| 100 |
+
# ── Add more LLM providers below ──────────────────────────────────────────
|
| 101 |
+
# [LLM_PROVIDER.mistral]
|
| 102 |
+
# active = "false"
|
| 103 |
+
# base_url = "https://api.mistral.ai/v1"
|
| 104 |
+
# env_key = "MISTRAL_API_KEY"
|
| 105 |
+
# default_model = "mistral-large-latest"
|
| 106 |
+
# models = "mistral-large-latest, mistral-small-latest"
|
| 107 |
+
# fallback_to = ""
|
| 108 |
+
# [LLM_PROVIDER.mistral_END]
|
| 109 |
+
|
| 110 |
+
# [LLM_PROVIDER.openai]
|
| 111 |
+
# active = "false"
|
| 112 |
+
# base_url = "https://api.openai.com/v1"
|
| 113 |
+
# env_key = "OPENAI_API_KEY"
|
| 114 |
+
# default_model = "gpt-4o"
|
| 115 |
+
# models = "gpt-4o, gpt-4o-mini, gpt-3.5-turbo"
|
| 116 |
+
# fallback_to = ""
|
| 117 |
+
# [LLM_PROVIDER.openai_END]
|
| 118 |
+
|
| 119 |
+
[LLM_PROVIDERS_END]
|
| 120 |
+
|
| 121 |
+
# ── Search Providers ───────────────────────────────────────────────────────────
|
| 122 |
+
[SEARCH_PROVIDERS]
|
| 123 |
+
|
| 124 |
+
[SEARCH_PROVIDER.brave]
|
| 125 |
+
active = "true"
|
| 126 |
+
base_url = "https://api.search.brave.com/res/v1/web/search"
|
| 127 |
+
env_key = "BRAVE_API_KEY" # → .env: BRAVE_API_KEY=BSA...
|
| 128 |
+
default_results = "5"
|
| 129 |
+
max_results = "20"
|
| 130 |
+
fallback_to = "tavily"
|
| 131 |
+
[SEARCH_PROVIDER.brave_END]
|
| 132 |
+
|
| 133 |
+
[SEARCH_PROVIDER.tavily]
|
| 134 |
+
active = "true"
|
| 135 |
+
base_url = "https://api.tavily.com/search"
|
| 136 |
+
env_key = "TAVILY_API_KEY" # → .env: TAVILY_API_KEY=tvly-...
|
| 137 |
+
default_results = "5"
|
| 138 |
+
max_results = "10"
|
| 139 |
+
include_answer = "true" # AI-synthesized answer
|
| 140 |
+
fallback_to = ""
|
| 141 |
+
[SEARCH_PROVIDER.tavily_END]
|
| 142 |
+
|
| 143 |
+
# ── Add more search providers below ───────────────────────────────────────
|
| 144 |
+
# [SEARCH_PROVIDER.serper]
|
| 145 |
+
# active = "false"
|
| 146 |
+
# base_url = "https://google.serper.dev/search"
|
| 147 |
+
# env_key = "SERPER_API_KEY"
|
| 148 |
+
# fallback_to = ""
|
| 149 |
+
# [SEARCH_PROVIDER.serper_END]
|
| 150 |
+
|
| 151 |
+
[SEARCH_PROVIDERS_END]
|
| 152 |
+
|
| 153 |
+
# ── Web / Action Providers (Webhooks, Bots, Social) ───────────────────────────
|
| 154 |
+
# [WEB_PROVIDERS]
|
| 155 |
+
|
| 156 |
+
# [WEB_PROVIDER.discord]
|
| 157 |
+
# active = "false"
|
| 158 |
+
# base_url = "https://discord.com/api/v10"
|
| 159 |
+
# env_key = "BOT_TOKEN"
|
| 160 |
+
# [WEB_PROVIDER.discord_END]
|
| 161 |
+
|
| 162 |
+
# [WEB_PROVIDER.github]
|
| 163 |
+
# active = "false"
|
| 164 |
+
# base_url = "https://api.github.com"
|
| 165 |
+
# env_key = "GITHUB_TOKEN"
|
| 166 |
+
# [WEB_PROVIDER.github_END]
|
| 167 |
+
|
| 168 |
+
# [WEB_PROVIDERS_END]
|
| 169 |
+
|
| 170 |
+
[PROVIDERS_END]
|
| 171 |
+
|
| 172 |
+
# =============================================================================
|
| 173 |
+
# MODELS — Token & rate limits per model
|
| 174 |
+
# Parser builds: MODELS[provider][model_name] → limits dict
|
| 175 |
+
# =============================================================================
|
| 176 |
+
[MODELS]
|
| 177 |
+
|
| 178 |
+
[MODEL.claude-opus-4-6]
|
| 179 |
+
provider = "anthropic"
|
| 180 |
+
context_tokens = "200000"
|
| 181 |
+
max_output_tokens = "32000"
|
| 182 |
+
requests_per_min = "5"
|
| 183 |
+
requests_per_day = "300"
|
| 184 |
+
cost_input_per_1k = "0.015" # USD — update as pricing changes
|
| 185 |
+
cost_output_per_1k = "0.075"
|
| 186 |
+
capabilities = "text, code, analysis, vision"
|
| 187 |
+
[MODEL.claude-opus-4-6_END]
|
| 188 |
+
|
| 189 |
+
[MODEL.claude-sonnet-4-6]
|
| 190 |
+
provider = "anthropic"
|
| 191 |
+
context_tokens = "200000"
|
| 192 |
+
max_output_tokens = "16000"
|
| 193 |
+
requests_per_min = "50"
|
| 194 |
+
requests_per_day = "1000"
|
| 195 |
+
cost_input_per_1k = "0.003"
|
| 196 |
+
cost_output_per_1k = "0.015"
|
| 197 |
+
capabilities = "text, code, analysis, vision"
|
| 198 |
+
[MODEL.claude-sonnet-4-6_END]
|
| 199 |
+
|
| 200 |
+
[MODEL.claude-haiku-4-5-20251001]
|
| 201 |
+
provider = "anthropic"
|
| 202 |
+
context_tokens = "200000"
|
| 203 |
+
max_output_tokens = "8000"
|
| 204 |
+
requests_per_min = "50"
|
| 205 |
+
requests_per_day = "2000"
|
| 206 |
+
cost_input_per_1k = "0.00025"
|
| 207 |
+
cost_output_per_1k = "0.00125"
|
| 208 |
+
capabilities = "text, code, fast"
|
| 209 |
+
[MODEL.claude-haiku-4-5-20251001_END]
|
| 210 |
+
|
| 211 |
+
[MODEL.gemini-2.0-flash]
|
| 212 |
+
provider = "gemini"
|
| 213 |
+
context_tokens = "1000000"
|
| 214 |
+
max_output_tokens = "8192"
|
| 215 |
+
requests_per_min = "15"
|
| 216 |
+
requests_per_day = "1500"
|
| 217 |
+
cost_input_per_1k = "0.00010"
|
| 218 |
+
cost_output_per_1k = "0.00040"
|
| 219 |
+
capabilities = "text, code, vision, audio"
|
| 220 |
+
[MODEL.gemini-2.0-flash_END]
|
| 221 |
+
|
| 222 |
+
[MODEL.gemini-1.5-pro]
|
| 223 |
+
provider = "gemini"
|
| 224 |
+
context_tokens = "2000000"
|
| 225 |
+
max_output_tokens = "8192"
|
| 226 |
+
requests_per_min = "2"
|
| 227 |
+
requests_per_day = "50"
|
| 228 |
+
cost_input_per_1k = "0.00125"
|
| 229 |
+
cost_output_per_1k = "0.00500"
|
| 230 |
+
capabilities = "text, code, vision, audio, long-context"
|
| 231 |
+
[MODEL.gemini-1.5-pro_END]
|
| 232 |
+
|
| 233 |
+
[MODEL.mistral-7b-instruct]
|
| 234 |
+
provider = "openrouter"
|
| 235 |
+
context_tokens = "32000"
|
| 236 |
+
max_output_tokens = "4096"
|
| 237 |
+
requests_per_min = "60"
|
| 238 |
+
requests_per_day = "10000"
|
| 239 |
+
cost_input_per_1k = "0.00006"
|
| 240 |
+
cost_output_per_1k = "0.00006"
|
| 241 |
+
capabilities = "text, code, fast, cheap"
|
| 242 |
+
[MODEL.mistral-7b-instruct_END]
|
| 243 |
+
|
| 244 |
+
[MODELS_END]
|
| 245 |
+
|
| 246 |
+
# =============================================================================
|
| 247 |
+
# TOOLS — Tool definitions + provider mapping
|
| 248 |
+
# Tools are registered in mcp.py only if their provider ENV key exists!
|
| 249 |
+
# =============================================================================
|
| 250 |
+
[TOOLS]
|
| 251 |
+
|
| 252 |
+
[TOOL.llm_complete]
|
| 253 |
+
active = "true"
|
| 254 |
+
description = "Send prompt to any configured LLM provider"
|
| 255 |
+
provider_type = "llm"
|
| 256 |
+
default_provider = "anthropic"
|
| 257 |
+
timeout_sec = "60"
|
| 258 |
+
[TOOL.llm_complete_END]
|
| 259 |
+
|
| 260 |
+
[TOOL.web_search]
|
| 261 |
+
active = "true"
|
| 262 |
+
description = "Search the web via configured search provider"
|
| 263 |
+
provider_type = "search"
|
| 264 |
+
default_provider = "brave"
|
| 265 |
+
timeout_sec = "30"
|
| 266 |
+
[TOOL.web_search_END]
|
| 267 |
+
|
| 268 |
+
[TOOL.db_query]
|
| 269 |
+
active = "true"
|
| 270 |
+
description = "Execute SELECT queries on connected database (read-only)"
|
| 271 |
+
provider_type = "db"
|
| 272 |
+
readonly = "true"
|
| 273 |
+
timeout_sec = "10"
|
| 274 |
+
[TOOL.db_query_END]
|
| 275 |
+
|
| 276 |
+
# ── Future tools ──────────────────────────────────────────────────────────
|
| 277 |
+
# [TOOL.image_gen]
|
| 278 |
+
# active = "false"
|
| 279 |
+
# description = "Generate images via configured provider"
|
| 280 |
+
# provider_type = "image"
|
| 281 |
+
# default_provider = ""
|
| 282 |
+
# timeout_sec = "120"
|
| 283 |
+
# [TOOL.image_gen_END]
|
| 284 |
+
|
| 285 |
+
# [TOOL.code_exec]
|
| 286 |
+
# active = "false"
|
| 287 |
+
# description = "Execute sandboxed code snippets"
|
| 288 |
+
# provider_type = "sandbox"
|
| 289 |
+
# timeout_sec = "30"
|
| 290 |
+
# [TOOL.code_exec_END]
|
| 291 |
+
|
| 292 |
+
[TOOLS_END]
|
| 293 |
+
|
| 294 |
+
# =============================================================================
|
| 295 |
+
# DB_SYNC — Internal SQLite config for app/* IPC
|
| 296 |
+
# This is NOT the cloud DB — that lives in .env → DATABASE_URL
|
| 297 |
+
# =============================================================================
|
| 298 |
+
[DB_SYNC]
|
| 299 |
+
SQLITE_PATH = "app/.hub_state.db" # internal state, never commit!
|
| 300 |
+
SYNC_INTERVAL_SEC = "30" # how often to flush to SQLite
|
| 301 |
+
MAX_CACHE_ENTRIES = "1000"
|
| 302 |
+
[DB_SYNC_END]
|
| 303 |
+
|
| 304 |
+
# =============================================================================
|
| 305 |
+
# DEBUG — app/* debug behavior (fundaments debug stays in .env)
|
| 306 |
+
# =============================================================================
|
| 307 |
+
[DEBUG]
|
| 308 |
+
DEBUG = "ON" # ON | OFF
|
| 309 |
+
DEBUG_LEVEL = "FULL" # FULL | WARN | ERROR
|
| 310 |
+
LOG_FILE = "hub_debug.log"
|
| 311 |
+
LOG_REQUESTS = "true" # log every provider request
|
| 312 |
+
LOG_RESPONSES = "false" # careful: may log sensitive data!
|
| 313 |
+
[DEBUG_END]
|
| 314 |
+
|
| 315 |
+
# =============================================================================
|
| 316 |
+
# PARSER_PROVIDER FREE ENDPOINTS — soon
|
| 317 |
+
# =============================================================================
|
| 318 |
+
[PARSER_PROVIDER]
|
| 319 |
+
#[PARSER_PROVIDER.metaculus]
|
| 320 |
+
#active = "true"
|
| 321 |
+
#base_url = "https://www.metaculus.com/api2"
|
| 322 |
+
#env_key = "" # public API, kein key!
|
| 323 |
+
#category = "research"
|
| 324 |
+
#legal_de = "true" # ← Deutschland-Check direkt in config!
|
| 325 |
+
#[PARSER_PROVIDER.metaculus_END]
|
| 326 |
+
|
| 327 |
+
#[PARSER_PROVIDER.polymarket]
|
| 328 |
+
#active = "false"
|
| 329 |
+
#base_url = "https://gamma-api.polymarket.com"
|
| 330 |
+
#env_key = ""
|
| 331 |
+
#legal_de = "false"
|
| 332 |
+
#[PARSER_PROVIDER.polymarket_END]
|
| 333 |
+
[PARSER_PROVIDER_END]
|
| 334 |
+
|
app/README.md
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
> Haha! Sandboxed MCP server! (No nelson image cause big "D")
|
| 2 |
+
|
app/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|
app/app.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# root/app/app.py
|
| 3 |
+
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
|
| 4 |
+
# Copyright 2026 - Volkan Kücükbudak
|
| 5 |
+
# Apache License V. 2 + ESOL 1.1
|
| 6 |
+
# Repo: https://github.com/VolkanSah/Universal-MCP-Hub-sandboxed
|
| 7 |
+
# =============================================================================
|
| 8 |
+
# ARCHITECTURE NOTE:
|
| 9 |
+
# This file is the Orchestrator of the sandboxed app/* layer.
|
| 10 |
+
# It is ONLY started by main.py (the "Guardian").
|
| 11 |
+
# All fundament services are injected via the `fundaments` dictionary.
|
| 12 |
+
# Direct execution is blocked by design.
|
| 13 |
+
#
|
| 14 |
+
# SANDBOX RULES:
|
| 15 |
+
# - fundaments dict is ONLY unpacked inside start_application()
|
| 16 |
+
# - fundaments are NEVER stored globally or passed to other app/* modules
|
| 17 |
+
# - app/* modules read their own config from app/.pyfun
|
| 18 |
+
# - app/* internal state/IPC uses app/db_sync.py (SQLite) — NOT postgresql.py
|
| 19 |
+
# - Secrets stay in .env → Guardian reads them → never touched by app/*
|
| 20 |
+
# =============================================================================
|
| 21 |
+
from quart import Quart, request, jsonify # async Flask — ASGI compatible
|
| 22 |
+
import logging
|
| 23 |
+
from hypercorn.asyncio import serve # ASGI server — async native, replaces waitress
|
| 24 |
+
from hypercorn.config import Config # hypercorn config
|
| 25 |
+
import threading # for future tools that need own threads
|
| 26 |
+
import requests # sync HTTP for future tool workers
|
| 27 |
+
import time
|
| 28 |
+
from datetime import datetime
|
| 29 |
+
import asyncio
|
| 30 |
+
from typing import Dict, Any, Optional
|
| 31 |
+
# =============================================================================
|
| 32 |
+
# Import app/* modules — MINIMAL BUILD
|
| 33 |
+
# Each module reads its own config from app/.pyfun independently.
|
| 34 |
+
# NO fundaments passed into these modules!
|
| 35 |
+
# =============================================================================
|
| 36 |
+
from . import mcp # MCP transport layer (SSE via Quart route)
|
| 37 |
+
from . import config as app_config # app/.pyfun parser — used only in app/*
|
| 38 |
+
from . import providers # API provider registry — reads app/.pyfun
|
| 39 |
+
from . import models # Model config + token/rate limits — reads app/.pyfun
|
| 40 |
+
from . import tools # MCP tool definitions + provider mapping — reads app/.pyfun
|
| 41 |
+
from . import db_sync # Internal SQLite IPC — app/* state & communication
|
| 42 |
+
# # db_sync ≠ postgresql.py! Cloud DB is Guardian-only.
|
| 43 |
+
# Future modules (will uncomment when ready):
|
| 44 |
+
# from . import discord_api # Discord bot integration
|
| 45 |
+
# from . import hf_hooks # HuggingFace Space hooks
|
| 46 |
+
# from . import git_hooks # GitHub/GitLab webhook handler
|
| 47 |
+
# from . import web_api # Generic REST API handler
|
| 48 |
+
# =============================================================================
|
| 49 |
+
# Loggers — one per module for clean log filtering
|
| 50 |
+
# =============================================================================
|
| 51 |
+
logger = logging.getLogger('application')
|
| 52 |
+
logger_mcp = logging.getLogger('mcp')
|
| 53 |
+
logger_config = logging.getLogger('config')
|
| 54 |
+
logger_tools = logging.getLogger('tools')
|
| 55 |
+
logger_providers = logging.getLogger('providers')
|
| 56 |
+
logger_models = logging.getLogger('models')
|
| 57 |
+
logger_db_sync = logging.getLogger('db_sync')
|
| 58 |
+
# =============================================================================
|
| 59 |
+
# Quart app instance
|
| 60 |
+
# =============================================================================
|
| 61 |
+
app = Quart(__name__)
|
| 62 |
+
START_TIME = datetime.utcnow()
|
| 63 |
+
# =============================================================================
|
| 64 |
+
# Quart Routes
|
| 65 |
+
# =============================================================================
|
| 66 |
+
@app.route("/", methods=["GET"])
|
| 67 |
+
async def health_check():
|
| 68 |
+
"""
|
| 69 |
+
Health check endpoint.
|
| 70 |
+
Used by HuggingFace Spaces and monitoring systems to verify the app is running.
|
| 71 |
+
"""
|
| 72 |
+
uptime = datetime.utcnow() - START_TIME
|
| 73 |
+
return jsonify({
|
| 74 |
+
"status": "running",
|
| 75 |
+
"service": "Universal MCP Hub",
|
| 76 |
+
"uptime_seconds": int(uptime.total_seconds()),
|
| 77 |
+
})
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@app.route("/api", methods=["POST"])
|
| 81 |
+
async def api_endpoint():
|
| 82 |
+
"""
|
| 83 |
+
Generic REST API endpoint for direct tool invocation.
|
| 84 |
+
Accepts JSON: { "tool": "tool_name", "params": { ... } }
|
| 85 |
+
Auth and validation handled by tools layer.
|
| 86 |
+
"""
|
| 87 |
+
# TODO: implement tool dispatch via tools.invoke()
|
| 88 |
+
data = await request.get_json()
|
| 89 |
+
return jsonify({"status": "not_implemented", "received": data}), 501
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
@app.route("/crypto", methods=["POST"])
|
| 93 |
+
async def crypto_endpoint():
|
| 94 |
+
"""
|
| 95 |
+
Encrypted API endpoint.
|
| 96 |
+
Encryption handled by app/* layer — no direct fundaments access here.
|
| 97 |
+
"""
|
| 98 |
+
# TODO: implement via app/* encryption wrapper
|
| 99 |
+
data = await request.get_json()
|
| 100 |
+
return jsonify({"status": "not_implemented"}), 501
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@app.route("/mcp", methods=["GET", "POST"])
|
| 104 |
+
async def mcp_endpoint():
|
| 105 |
+
"""
|
| 106 |
+
MCP SSE Transport endpoint — routed through Quart/hypercorn.
|
| 107 |
+
All MCP traffic passes through here — enables interception, logging,
|
| 108 |
+
auth checks, rate limiting, payload transformation before reaching MCP.
|
| 109 |
+
"""
|
| 110 |
+
return await mcp.handle_request(request)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# Future routes (uncomment when ready):
|
| 114 |
+
# @app.route("/discord", methods=["POST"])
|
| 115 |
+
# async def discord_interactions():
|
| 116 |
+
# """Discord interactions endpoint — signature verification via discord_api module."""
|
| 117 |
+
# pass
|
| 118 |
+
|
| 119 |
+
# @app.route("/webhook/hf", methods=["POST"])
|
| 120 |
+
# async def hf_webhook():
|
| 121 |
+
# """HuggingFace Space event hooks."""
|
| 122 |
+
# pass
|
| 123 |
+
|
| 124 |
+
# @app.route("/webhook/git", methods=["POST"])
|
| 125 |
+
# async def git_webhook():
|
| 126 |
+
# """GitHub / GitLab webhook handler."""
|
| 127 |
+
# pass
|
| 128 |
+
# =============================================================================
|
| 129 |
+
# Main entry point — called exclusively by Guardian (main.py)
|
| 130 |
+
# =============================================================================
|
| 131 |
+
async def start_application(fundaments: Dict[str, Any]) -> None:
|
| 132 |
+
"""
|
| 133 |
+
Main entry point for the sandboxed app layer.
|
| 134 |
+
Called exclusively by main.py after all fundament services are initialized.
|
| 135 |
+
|
| 136 |
+
Args:
|
| 137 |
+
fundaments: Dictionary of initialized services from Guardian (main.py).
|
| 138 |
+
Services are unpacked here and NEVER stored globally or
|
| 139 |
+
passed into other app/* modules.
|
| 140 |
+
"""
|
| 141 |
+
logger.info("Application starting...")
|
| 142 |
+
|
| 143 |
+
# =========================================================================
|
| 144 |
+
# Unpack fundaments — ONLY here, NEVER elsewhere in app/*
|
| 145 |
+
# These are the 6 fundament services from fundaments/*
|
| 146 |
+
# =========================================================================
|
| 147 |
+
config_service = fundaments["config"] # fundaments/config_handler.py
|
| 148 |
+
db_service = fundaments["db"] # fundaments/postgresql.py — None if not configured
|
| 149 |
+
encryption_service = fundaments["encryption"] # fundaments/encryption.py — None if keys not set
|
| 150 |
+
access_control_service = fundaments["access_control"] # fundaments/access_control.py — None if no DB
|
| 151 |
+
user_handler_service = fundaments["user_handler"] # fundaments/user_handler.py — None if no DB
|
| 152 |
+
security_service = fundaments["security"] # fundaments/security.py — None if deps missing
|
| 153 |
+
|
| 154 |
+
# --- Log active fundament services ---
|
| 155 |
+
if encryption_service:
|
| 156 |
+
logger.info("Encryption service active.")
|
| 157 |
+
|
| 158 |
+
if user_handler_service and security_service:
|
| 159 |
+
logger.info("Auth services active (user_handler + security).")
|
| 160 |
+
|
| 161 |
+
if access_control_service and security_service:
|
| 162 |
+
logger.info("Access control active.")
|
| 163 |
+
|
| 164 |
+
if db_service and not user_handler_service:
|
| 165 |
+
logger.info("Database-only mode active (e.g. ML pipeline).")
|
| 166 |
+
|
| 167 |
+
if not db_service:
|
| 168 |
+
logger.info("Database-free mode active (e.g. Discord bot, API client).")
|
| 169 |
+
|
| 170 |
+
# =========================================================================
|
| 171 |
+
# Initialize app/* internal services — MINIMAL BUILD
|
| 172 |
+
# Uncomment each line when the module is ready!
|
| 173 |
+
# =========================================================================
|
| 174 |
+
# await db_sync.initialize() # SQLite IPC store for app/* — unrelated to postgresql.py
|
| 175 |
+
# await providers.initialize() # reads app/.pyfun [LLM_PROVIDERS] [SEARCH_PROVIDERS] # in mcp_init
|
| 176 |
+
# await models.initialize() # reads app/.pyfun [MODELS] # in mcp_init
|
| 177 |
+
# await tools.initialize() # reads app/.pyfun [TOOLS]
|
| 178 |
+
|
| 179 |
+
# --- Initialize MCP (registers tools, prepares SSE handler) ---
|
| 180 |
+
# db_sync only if cloud_DB used to!
|
| 181 |
+
# await db_sync.initialize()
|
| 182 |
+
await mcp.initialize()
|
| 183 |
+
|
| 184 |
+
# --- Read PORT from app/.pyfun [HUB] ---
|
| 185 |
+
port = int(app_config.get_hub().get("HUB_PORT", "7860"))
|
| 186 |
+
|
| 187 |
+
# --- Configure hypercorn ---
|
| 188 |
+
config = Config()
|
| 189 |
+
config.bind = [f"0.0.0.0:{port}"]
|
| 190 |
+
|
| 191 |
+
logger.info(f"Starting hypercorn on port {port}...")
|
| 192 |
+
logger.info("All services running.")
|
| 193 |
+
|
| 194 |
+
# --- Run hypercorn — blocks until shutdown ---
|
| 195 |
+
await serve(app, config)
|
| 196 |
+
|
| 197 |
+
# =============================================================================
|
| 198 |
+
# Direct execution guard
|
| 199 |
+
# =============================================================================
|
| 200 |
+
if __name__ == '__main__':
|
| 201 |
+
print("WARNING: Running app.py directly. Fundament modules might not be correctly initialized.")
|
| 202 |
+
print("Please run 'python main.py' instead for proper initialization.")
|
| 203 |
+
|
| 204 |
+
test_fundaments = {
|
| 205 |
+
"config": None,
|
| 206 |
+
"db": None,
|
| 207 |
+
"encryption": None,
|
| 208 |
+
"access_control": None,
|
| 209 |
+
"user_handler": None,
|
| 210 |
+
"security": None,
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
asyncio.run(start_application(test_fundaments))
|
app/config.py
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# app/config.py
|
| 3 |
+
# .pyfun parser for app/* modules
|
| 4 |
+
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
|
| 5 |
+
# Copyright 2026 - Volkan Kücükbudak
|
| 6 |
+
# Apache License V. 2 + ESOL 1.1
|
| 7 |
+
# =============================================================================
|
| 8 |
+
# USAGE in any app/* module:
|
| 9 |
+
# from . import config
|
| 10 |
+
# cfg = config.get()
|
| 11 |
+
# providers = cfg["LLM_PROVIDERS"]
|
| 12 |
+
# =============================================================================
|
| 13 |
+
# USAGE
|
| 14 |
+
# in providers.py
|
| 15 |
+
# from . import config
|
| 16 |
+
|
| 17 |
+
# active = config.get_active_llm_providers()
|
| 18 |
+
# → { "anthropic": { "base_url": "...", "env_key": "ANTHROPIC_API_KEY", ... }, ... }
|
| 19 |
+
# =============================================================================
|
| 20 |
+
# in models.py
|
| 21 |
+
# from . import config
|
| 22 |
+
|
| 23 |
+
# anthropic_models = config.get_models_for_provider("anthropic")
|
| 24 |
+
# =============================================================================
|
| 25 |
+
# in tools.py
|
| 26 |
+
# from . import config
|
| 27 |
+
|
| 28 |
+
# active_tools = config.get_active_tools()
|
| 29 |
+
# =============================================================================
|
| 30 |
+
import os
|
| 31 |
+
import logging
|
| 32 |
+
from typing import Dict, Any, Optional
|
| 33 |
+
|
| 34 |
+
logger = logging.getLogger('app.config')
|
| 35 |
+
|
| 36 |
+
# Path to .pyfun — lives in app/ next to this file
|
| 37 |
+
PYFUN_PATH = os.path.join(os.path.dirname(__file__), ".pyfun")
|
| 38 |
+
|
| 39 |
+
# Internal cache — loaded once at first get()
|
| 40 |
+
_cache: Optional[Dict[str, Any]] = None
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _parse_value(value: str) -> str:
|
| 44 |
+
"""Strip quotes and inline comments from a value."""
|
| 45 |
+
value = value.strip()
|
| 46 |
+
# Remove inline comment
|
| 47 |
+
if " #" in value:
|
| 48 |
+
value = value[:value.index(" #")].strip()
|
| 49 |
+
# Strip surrounding quotes
|
| 50 |
+
if value.startswith('"') and value.endswith('"'):
|
| 51 |
+
value = value[1:-1]
|
| 52 |
+
return value
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def _parse() -> Dict[str, Any]:
|
| 56 |
+
"""
|
| 57 |
+
Parses the app/.pyfun file into a nested dictionary.
|
| 58 |
+
|
| 59 |
+
Structure:
|
| 60 |
+
[SECTION]
|
| 61 |
+
[SUBSECTION]
|
| 62 |
+
[BLOCK.name]
|
| 63 |
+
key = "value"
|
| 64 |
+
[BLOCK.name_END]
|
| 65 |
+
[SUBSECTION_END]
|
| 66 |
+
[SECTION_END]
|
| 67 |
+
|
| 68 |
+
Returns nested dict:
|
| 69 |
+
{
|
| 70 |
+
"HUB": { "HUB_NAME": "...", ... },
|
| 71 |
+
"LLM_PROVIDERS": {
|
| 72 |
+
"anthropic": { "active": "true", "base_url": "...", ... },
|
| 73 |
+
"gemini": { ... },
|
| 74 |
+
},
|
| 75 |
+
"MODELS": {
|
| 76 |
+
"claude-opus-4-6": { "provider": "anthropic", ... },
|
| 77 |
+
},
|
| 78 |
+
...
|
| 79 |
+
}
|
| 80 |
+
"""
|
| 81 |
+
if not os.path.isfile(PYFUN_PATH):
|
| 82 |
+
logger.critical(f".pyfun not found at: {PYFUN_PATH}")
|
| 83 |
+
raise FileNotFoundError(f".pyfun not found at: {PYFUN_PATH}")
|
| 84 |
+
|
| 85 |
+
result: Dict[str, Any] = {}
|
| 86 |
+
|
| 87 |
+
# Parser state
|
| 88 |
+
section: Optional[str] = None # e.g. "HUB", "PROVIDERS"
|
| 89 |
+
subsection: Optional[str] = None # e.g. "LLM_PROVIDERS"
|
| 90 |
+
block_type: Optional[str] = None # e.g. "LLM_PROVIDER", "MODEL", "TOOL"
|
| 91 |
+
block_name: Optional[str] = None # e.g. "anthropic", "claude-opus-4-6"
|
| 92 |
+
|
| 93 |
+
with open(PYFUN_PATH, "r", encoding="utf-8") as f:
|
| 94 |
+
for raw_line in f:
|
| 95 |
+
line = raw_line.strip()
|
| 96 |
+
|
| 97 |
+
# Skip empty lines and full-line comments
|
| 98 |
+
if not line or line.startswith("#"):
|
| 99 |
+
continue
|
| 100 |
+
|
| 101 |
+
# Skip file identifier
|
| 102 |
+
if line.startswith("[PYFUN_FILE"):
|
| 103 |
+
continue
|
| 104 |
+
|
| 105 |
+
# --- Block END markers (most specific first) ---
|
| 106 |
+
if line.endswith("_END]") and "." in line:
|
| 107 |
+
# e.g. [LLM_PROVIDER.anthropic_END] or [MODEL.claude-opus-4-6_END]
|
| 108 |
+
block_type = None
|
| 109 |
+
block_name = None
|
| 110 |
+
continue
|
| 111 |
+
|
| 112 |
+
if line.endswith("_END]") and not "." in line:
|
| 113 |
+
# e.g. [LLM_PROVIDERS_END], [HUB_END], [MODELS_END]
|
| 114 |
+
inner = line[1:-1].replace("_END", "")
|
| 115 |
+
if subsection and inner == subsection:
|
| 116 |
+
subsection = None
|
| 117 |
+
elif section and inner == section:
|
| 118 |
+
section = None
|
| 119 |
+
continue
|
| 120 |
+
|
| 121 |
+
# --- Block START markers ---
|
| 122 |
+
if line.startswith("[") and line.endswith("]"):
|
| 123 |
+
inner = line[1:-1]
|
| 124 |
+
|
| 125 |
+
# Named block: [LLM_PROVIDER.anthropic] or [MODEL.claude-opus-4-6]
|
| 126 |
+
if "." in inner:
|
| 127 |
+
parts = inner.split(".", 1)
|
| 128 |
+
block_type = parts[0] # e.g. LLM_PROVIDER, MODEL, TOOL
|
| 129 |
+
block_name = parts[1] # e.g. anthropic, claude-opus-4-6
|
| 130 |
+
|
| 131 |
+
# Determine which top-level key to store under
|
| 132 |
+
if block_type == "LLM_PROVIDER":
|
| 133 |
+
result.setdefault("LLM_PROVIDERS", {})
|
| 134 |
+
result["LLM_PROVIDERS"].setdefault(block_name, {})
|
| 135 |
+
elif block_type == "SEARCH_PROVIDER":
|
| 136 |
+
result.setdefault("SEARCH_PROVIDERS", {})
|
| 137 |
+
result["SEARCH_PROVIDERS"].setdefault(block_name, {})
|
| 138 |
+
elif block_type == "WEB_PROVIDER":
|
| 139 |
+
result.setdefault("WEB_PROVIDERS", {})
|
| 140 |
+
result["WEB_PROVIDERS"].setdefault(block_name, {})
|
| 141 |
+
elif block_type == "MODEL":
|
| 142 |
+
result.setdefault("MODELS", {})
|
| 143 |
+
result["MODELS"].setdefault(block_name, {})
|
| 144 |
+
elif block_type == "TOOL":
|
| 145 |
+
result.setdefault("TOOLS", {})
|
| 146 |
+
result["TOOLS"].setdefault(block_name, {})
|
| 147 |
+
continue
|
| 148 |
+
|
| 149 |
+
# Subsection: [LLM_PROVIDERS], [SEARCH_PROVIDERS] etc.
|
| 150 |
+
if section and not subsection:
|
| 151 |
+
subsection = inner
|
| 152 |
+
result.setdefault(inner, {})
|
| 153 |
+
continue
|
| 154 |
+
|
| 155 |
+
# Top-level section: [HUB], [PROVIDERS], [MODELS] etc.
|
| 156 |
+
section = inner
|
| 157 |
+
result.setdefault(inner, {})
|
| 158 |
+
continue
|
| 159 |
+
|
| 160 |
+
# --- Key = Value ---
|
| 161 |
+
if "=" in line:
|
| 162 |
+
key, _, val = line.partition("=")
|
| 163 |
+
key = key.strip()
|
| 164 |
+
val = _parse_value(val)
|
| 165 |
+
|
| 166 |
+
# Strip provider prefix from key (e.g. "anthropic.base_url" → "base_url")
|
| 167 |
+
if block_name and key.startswith(f"{block_name}."):
|
| 168 |
+
key = key[len(block_name) + 1:]
|
| 169 |
+
|
| 170 |
+
# Store in correct location
|
| 171 |
+
if block_type and block_name:
|
| 172 |
+
if block_type == "LLM_PROVIDER":
|
| 173 |
+
result["LLM_PROVIDERS"][block_name][key] = val
|
| 174 |
+
elif block_type == "SEARCH_PROVIDER":
|
| 175 |
+
result["SEARCH_PROVIDERS"][block_name][key] = val
|
| 176 |
+
elif block_type == "WEB_PROVIDER":
|
| 177 |
+
result["WEB_PROVIDERS"][block_name][key] = val
|
| 178 |
+
elif block_type == "MODEL":
|
| 179 |
+
result["MODELS"][block_name][key] = val
|
| 180 |
+
elif block_type == "TOOL":
|
| 181 |
+
result["TOOLS"][block_name][key] = val
|
| 182 |
+
elif section:
|
| 183 |
+
result[section][key] = val
|
| 184 |
+
|
| 185 |
+
logger.info(f".pyfun loaded. Sections: {list(result.keys())}")
|
| 186 |
+
return result
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def load() -> Dict[str, Any]:
|
| 190 |
+
"""Force (re)load of .pyfun — clears cache."""
|
| 191 |
+
global _cache
|
| 192 |
+
_cache = _parse()
|
| 193 |
+
return _cache
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def get() -> Dict[str, Any]:
|
| 197 |
+
"""
|
| 198 |
+
Returns parsed .pyfun config as nested dict.
|
| 199 |
+
Loads and caches on first call — subsequent calls return cache.
|
| 200 |
+
"""
|
| 201 |
+
global _cache
|
| 202 |
+
if _cache is None:
|
| 203 |
+
_cache = _parse()
|
| 204 |
+
return _cache
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def get_section(section: str) -> Dict[str, Any]:
|
| 208 |
+
"""
|
| 209 |
+
Returns a specific top-level section.
|
| 210 |
+
Returns empty dict if section not found.
|
| 211 |
+
"""
|
| 212 |
+
return get().get(section, {})
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def get_llm_providers() -> Dict[str, Any]:
|
| 216 |
+
"""Returns all LLM providers (active and inactive)."""
|
| 217 |
+
return get().get("LLM_PROVIDERS", {})
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def get_active_llm_providers() -> Dict[str, Any]:
|
| 221 |
+
"""Returns only LLM providers where active = 'true'."""
|
| 222 |
+
return {
|
| 223 |
+
name: cfg
|
| 224 |
+
for name, cfg in get_llm_providers().items()
|
| 225 |
+
if cfg.get("active", "false").lower() == "true"
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def get_search_providers() -> Dict[str, Any]:
|
| 230 |
+
"""Returns all search providers."""
|
| 231 |
+
return get().get("SEARCH_PROVIDERS", {})
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def get_active_search_providers() -> Dict[str, Any]:
|
| 235 |
+
"""Returns only search providers where active = 'true'."""
|
| 236 |
+
return {
|
| 237 |
+
name: cfg
|
| 238 |
+
for name, cfg in get_search_providers().items()
|
| 239 |
+
if cfg.get("active", "false").lower() == "true"
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def get_models() -> Dict[str, Any]:
|
| 244 |
+
"""Returns all model definitions."""
|
| 245 |
+
return get().get("MODELS", {})
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def get_models_for_provider(provider_name: str) -> Dict[str, Any]:
|
| 249 |
+
"""Returns all models for a specific provider."""
|
| 250 |
+
return {
|
| 251 |
+
name: cfg
|
| 252 |
+
for name, cfg in get_models().items()
|
| 253 |
+
if cfg.get("provider", "") == provider_name
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def get_tools() -> Dict[str, Any]:
|
| 258 |
+
"""Returns all tool definitions."""
|
| 259 |
+
return get().get("TOOLS", {})
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def get_active_tools() -> Dict[str, Any]:
|
| 263 |
+
"""Returns only tools where active = 'true'."""
|
| 264 |
+
return {
|
| 265 |
+
name: cfg
|
| 266 |
+
for name, cfg in get_tools().items()
|
| 267 |
+
if cfg.get("active", "false").lower() == "true"
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
def get_hub() -> Dict[str, Any]:
|
| 272 |
+
"""Returns [HUB] section."""
|
| 273 |
+
return get_section("HUB")
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
def get_limits() -> Dict[str, Any]:
|
| 277 |
+
"""Returns [HUB_LIMITS] section."""
|
| 278 |
+
return get_section("HUB_LIMITS")
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
def get_db_sync() -> Dict[str, Any]:
|
| 282 |
+
"""Returns [DB_SYNC] section."""
|
| 283 |
+
return get_section("DB_SYNC")
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
def get_debug() -> Dict[str, Any]:
|
| 287 |
+
"""Returns [DEBUG] section."""
|
| 288 |
+
return get_section("DEBUG")
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
def is_debug() -> bool:
|
| 292 |
+
"""Returns True if DEBUG = 'ON' in .pyfun."""
|
| 293 |
+
return get_debug().get("DEBUG", "OFF").upper() == "ON"
|
app/db_sync.py
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# =============================================================================
|
| 3 |
+
# app/db_sync.py
|
| 4 |
+
# Internal SQLite IPC — app/* state & communication
|
| 5 |
+
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
|
| 6 |
+
# Copyright 2026 - Volkan Kücükbudak
|
| 7 |
+
# Apache License V. 2 + ESOL 1.1
|
| 8 |
+
# Repo: https://github.com/VolkanSah/Universal-MCP-Hub-sandboxed
|
| 9 |
+
# =============================================================================
|
| 10 |
+
# ARCHITECTURE NOTE:
|
| 11 |
+
# This file lives exclusively in app/ and is ONLY started by app/app.py.
|
| 12 |
+
# NO direct access to fundaments/*, .env, or Guardian (main.py).
|
| 13 |
+
# DB path comes from app/.pyfun [DB_SYNC] → SQLITE_PATH via app/config.py.
|
| 14 |
+
#
|
| 15 |
+
# CRITICAL RULES:
|
| 16 |
+
# - This is NOT postgresql.py — cloud DB is Guardian-only!
|
| 17 |
+
# - db_sync ONLY manages its own tables (hub_state, tool_cache)
|
| 18 |
+
# - NEVER touch Guardian tables (users, sessions) — those belong to user_handler.py
|
| 19 |
+
# - SQLite path is shared with user_handler.py via SQLITE_PATH
|
| 20 |
+
# - app/* modules call db_sync.write() / db_sync.read() — never aiosqlite directly
|
| 21 |
+
#
|
| 22 |
+
# TABLE OWNERSHIP:
|
| 23 |
+
# users, sessions → Guardian (fundaments/user_handler.py) — DO NOT TOUCH!
|
| 24 |
+
# hub_state → db_sync (app/* internal state)
|
| 25 |
+
# tool_cache → db_sync (app/* tool response cache)
|
| 26 |
+
# =============================================================================
|
| 27 |
+
|
| 28 |
+
import aiosqlite
|
| 29 |
+
import logging
|
| 30 |
+
import json
|
| 31 |
+
from datetime import datetime, timezone
|
| 32 |
+
from typing import Any, Dict, List, Optional
|
| 33 |
+
|
| 34 |
+
from . import config
|
| 35 |
+
|
| 36 |
+
logger = logging.getLogger("db_sync")
|
| 37 |
+
|
| 38 |
+
# =============================================================================
|
| 39 |
+
# Internal State
|
| 40 |
+
# =============================================================================
|
| 41 |
+
_db_path: Optional[str] = None
|
| 42 |
+
_initialized: bool = False
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# =============================================================================
|
| 46 |
+
# Initialization — called by app/app.py (parameterless, sandboxed)
|
| 47 |
+
# =============================================================================
|
| 48 |
+
|
| 49 |
+
async def initialize() -> None:
|
| 50 |
+
"""
|
| 51 |
+
Initializes db_sync — reads SQLITE_PATH from .pyfun [DB_SYNC].
|
| 52 |
+
Creates app/* tables if they don't exist.
|
| 53 |
+
Called once by app/app.py during startup sequence.
|
| 54 |
+
No fundaments passed in — fully sandboxed.
|
| 55 |
+
|
| 56 |
+
Shares the SQLite file with user_handler.py (Guardian) but only
|
| 57 |
+
manages its own tables. Guardian tables are never touched here.
|
| 58 |
+
"""
|
| 59 |
+
global _db_path, _initialized
|
| 60 |
+
|
| 61 |
+
if _initialized:
|
| 62 |
+
return
|
| 63 |
+
|
| 64 |
+
db_cfg = config.get_db_sync()
|
| 65 |
+
_db_path = db_cfg.get("SQLITE_PATH", "app/.hub_state.db")
|
| 66 |
+
|
| 67 |
+
await _init_tables()
|
| 68 |
+
|
| 69 |
+
_initialized = True
|
| 70 |
+
logger.info(f"db_sync initialized — path: {_db_path}")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# =============================================================================
|
| 74 |
+
# SECTION 1 — Table Setup (app/* tables only!)
|
| 75 |
+
# =============================================================================
|
| 76 |
+
|
| 77 |
+
async def _init_tables() -> None:
|
| 78 |
+
"""
|
| 79 |
+
Creates app/* internal tables if they don't exist.
|
| 80 |
+
NEVER modifies Guardian tables (users, sessions).
|
| 81 |
+
"""
|
| 82 |
+
async with aiosqlite.connect(_db_path) as db:
|
| 83 |
+
|
| 84 |
+
# hub_state — generic key/value store for app/* modules
|
| 85 |
+
await db.execute("""
|
| 86 |
+
CREATE TABLE IF NOT EXISTS hub_state (
|
| 87 |
+
key TEXT PRIMARY KEY,
|
| 88 |
+
value TEXT,
|
| 89 |
+
updated_at TEXT
|
| 90 |
+
)
|
| 91 |
+
""")
|
| 92 |
+
|
| 93 |
+
# tool_cache — cached tool responses to reduce API calls
|
| 94 |
+
await db.execute("""
|
| 95 |
+
CREATE TABLE IF NOT EXISTS tool_cache (
|
| 96 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 97 |
+
tool_name TEXT NOT NULL,
|
| 98 |
+
prompt TEXT NOT NULL,
|
| 99 |
+
response TEXT NOT NULL,
|
| 100 |
+
provider TEXT,
|
| 101 |
+
model TEXT,
|
| 102 |
+
created_at TEXT
|
| 103 |
+
)
|
| 104 |
+
""")
|
| 105 |
+
|
| 106 |
+
await db.execute("""
|
| 107 |
+
CREATE INDEX IF NOT EXISTS idx_tool_cache_tool
|
| 108 |
+
ON tool_cache(tool_name)
|
| 109 |
+
""")
|
| 110 |
+
|
| 111 |
+
await db.commit()
|
| 112 |
+
|
| 113 |
+
logger.info("db_sync tables ready.")
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# =============================================================================
|
| 117 |
+
# SECTION 2 — Key/Value Store (hub_state table)
|
| 118 |
+
# =============================================================================
|
| 119 |
+
|
| 120 |
+
async def write(key: str, value: Any) -> None:
|
| 121 |
+
"""
|
| 122 |
+
Write a value to hub_state key/value store.
|
| 123 |
+
Value is JSON-serialized — supports dicts, lists, strings, numbers.
|
| 124 |
+
|
| 125 |
+
Args:
|
| 126 |
+
key: Unique key string (e.g. 'scheduler.last_run').
|
| 127 |
+
value: Any JSON-serializable value.
|
| 128 |
+
"""
|
| 129 |
+
_check_init()
|
| 130 |
+
now = datetime.now(timezone.utc).isoformat()
|
| 131 |
+
|
| 132 |
+
async with aiosqlite.connect(_db_path) as db:
|
| 133 |
+
await db.execute("""
|
| 134 |
+
INSERT OR REPLACE INTO hub_state (key, value, updated_at)
|
| 135 |
+
VALUES (?, ?, ?)
|
| 136 |
+
""", (key, json.dumps(value), now))
|
| 137 |
+
await db.commit()
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
async def read(key: str, default: Any = None) -> Any:
|
| 141 |
+
"""
|
| 142 |
+
Read a value from hub_state key/value store.
|
| 143 |
+
Returns default if key does not exist.
|
| 144 |
+
|
| 145 |
+
Args:
|
| 146 |
+
key: Key string to look up.
|
| 147 |
+
default: Default value if key not found. Default: None.
|
| 148 |
+
|
| 149 |
+
Returns:
|
| 150 |
+
Deserialized value, or default if not found.
|
| 151 |
+
"""
|
| 152 |
+
_check_init()
|
| 153 |
+
|
| 154 |
+
async with aiosqlite.connect(_db_path) as db:
|
| 155 |
+
cursor = await db.execute(
|
| 156 |
+
"SELECT value FROM hub_state WHERE key = ?", (key,)
|
| 157 |
+
)
|
| 158 |
+
row = await cursor.fetchone()
|
| 159 |
+
|
| 160 |
+
if row is None:
|
| 161 |
+
return default
|
| 162 |
+
|
| 163 |
+
try:
|
| 164 |
+
return json.loads(row[0])
|
| 165 |
+
except (json.JSONDecodeError, TypeError):
|
| 166 |
+
return row[0]
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
async def delete(key: str) -> None:
|
| 170 |
+
"""
|
| 171 |
+
Delete a key from hub_state.
|
| 172 |
+
|
| 173 |
+
Args:
|
| 174 |
+
key: Key string to delete.
|
| 175 |
+
"""
|
| 176 |
+
_check_init()
|
| 177 |
+
|
| 178 |
+
async with aiosqlite.connect(_db_path) as db:
|
| 179 |
+
await db.execute("DELETE FROM hub_state WHERE key = ?", (key,))
|
| 180 |
+
await db.commit()
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# =============================================================================
|
| 184 |
+
# SECTION 3 — Tool Cache (tool_cache table)
|
| 185 |
+
# =============================================================================
|
| 186 |
+
|
| 187 |
+
async def cache_write(
|
| 188 |
+
tool_name: str,
|
| 189 |
+
prompt: str,
|
| 190 |
+
response: str,
|
| 191 |
+
provider: str = None,
|
| 192 |
+
model: str = None,
|
| 193 |
+
) -> None:
|
| 194 |
+
"""
|
| 195 |
+
Cache a tool response to reduce redundant API calls.
|
| 196 |
+
|
| 197 |
+
Args:
|
| 198 |
+
tool_name: Tool name (e.g. 'llm_complete', 'web_search').
|
| 199 |
+
prompt: The input prompt/query that was used.
|
| 200 |
+
response: The response to cache.
|
| 201 |
+
provider: Provider name used (optional).
|
| 202 |
+
model: Model name used (optional).
|
| 203 |
+
"""
|
| 204 |
+
_check_init()
|
| 205 |
+
|
| 206 |
+
db_cfg = config.get_db_sync()
|
| 207 |
+
max_entries = int(db_cfg.get("MAX_CACHE_ENTRIES", "1000"))
|
| 208 |
+
now = datetime.now(timezone.utc).isoformat()
|
| 209 |
+
|
| 210 |
+
async with aiosqlite.connect(_db_path) as db:
|
| 211 |
+
await db.execute("""
|
| 212 |
+
INSERT INTO tool_cache (tool_name, prompt, response, provider, model, created_at)
|
| 213 |
+
VALUES (?, ?, ?, ?, ?, ?)
|
| 214 |
+
""", (tool_name, prompt, response, provider, model, now))
|
| 215 |
+
|
| 216 |
+
# Enforce MAX_CACHE_ENTRIES — delete oldest if exceeded
|
| 217 |
+
await db.execute("""
|
| 218 |
+
DELETE FROM tool_cache WHERE id NOT IN (
|
| 219 |
+
SELECT id FROM tool_cache ORDER BY created_at DESC LIMIT ?
|
| 220 |
+
)
|
| 221 |
+
""", (max_entries,))
|
| 222 |
+
|
| 223 |
+
await db.commit()
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
async def cache_read(tool_name: str, prompt: str) -> Optional[str]:
|
| 227 |
+
"""
|
| 228 |
+
Read a cached tool response.
|
| 229 |
+
Returns None if no cache entry exists.
|
| 230 |
+
|
| 231 |
+
Args:
|
| 232 |
+
tool_name: Tool name to look up.
|
| 233 |
+
prompt: The exact prompt/query to match.
|
| 234 |
+
|
| 235 |
+
Returns:
|
| 236 |
+
Cached response string, or None if not found.
|
| 237 |
+
"""
|
| 238 |
+
_check_init()
|
| 239 |
+
|
| 240 |
+
async with aiosqlite.connect(_db_path) as db:
|
| 241 |
+
cursor = await db.execute("""
|
| 242 |
+
SELECT response FROM tool_cache
|
| 243 |
+
WHERE tool_name = ? AND prompt = ?
|
| 244 |
+
ORDER BY created_at DESC LIMIT 1
|
| 245 |
+
""", (tool_name, prompt))
|
| 246 |
+
row = await cursor.fetchone()
|
| 247 |
+
|
| 248 |
+
return row[0] if row else None
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
# =============================================================================
|
| 252 |
+
# SECTION 4 — Read-Only Query (for mcp.py db_query tool)
|
| 253 |
+
# =============================================================================
|
| 254 |
+
|
| 255 |
+
async def query(sql: str) -> List[Dict]:
|
| 256 |
+
"""
|
| 257 |
+
Execute a read-only SELECT query on the internal hub state database.
|
| 258 |
+
Only SELECT statements are permitted — write operations are blocked.
|
| 259 |
+
Called by mcp.py db_query tool when db_sync.py is active.
|
| 260 |
+
|
| 261 |
+
Args:
|
| 262 |
+
sql: SQL SELECT statement to execute.
|
| 263 |
+
|
| 264 |
+
Returns:
|
| 265 |
+
List of result rows as dicts.
|
| 266 |
+
|
| 267 |
+
Raises:
|
| 268 |
+
ValueError: If the query is not a SELECT statement.
|
| 269 |
+
"""
|
| 270 |
+
_check_init()
|
| 271 |
+
|
| 272 |
+
if not sql.strip().upper().startswith("SELECT"):
|
| 273 |
+
raise ValueError("Only SELECT queries are permitted in db_query tool.")
|
| 274 |
+
|
| 275 |
+
async with aiosqlite.connect(_db_path) as db:
|
| 276 |
+
db.row_factory = aiosqlite.Row
|
| 277 |
+
cursor = await db.execute(sql)
|
| 278 |
+
rows = await cursor.fetchall()
|
| 279 |
+
return [dict(r) for r in rows]
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
# =============================================================================
|
| 283 |
+
# SECTION 5 — Helpers
|
| 284 |
+
# =============================================================================
|
| 285 |
+
|
| 286 |
+
def _check_init() -> None:
|
| 287 |
+
"""Raise RuntimeError if db_sync was not initialized."""
|
| 288 |
+
if not _initialized or not _db_path:
|
| 289 |
+
raise RuntimeError("db_sync not initialized — call initialize() first.")
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def is_ready() -> bool:
|
| 293 |
+
"""Returns True if db_sync is initialized and ready."""
|
| 294 |
+
return _initialized and _db_path is not None
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
# =============================================================================
|
| 298 |
+
# Direct execution guard
|
| 299 |
+
# =============================================================================
|
| 300 |
+
|
| 301 |
+
if __name__ == "__main__":
|
| 302 |
+
print("WARNING: Run via main.py → app.py, not directly.")
|
app/mcp.py
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# root/app/mcp.py
|
| 3 |
+
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
|
| 4 |
+
# Copyright 2026 - Volkan Kücükbudak
|
| 5 |
+
# Apache License V. 2 + ESOL 1.1
|
| 6 |
+
# Repo: https://github.com/VolkanSah/Universal-MCP-Hub-sandboxed
|
| 7 |
+
# =============================================================================
|
| 8 |
+
# ARCHITECTURE NOTE:
|
| 9 |
+
# This file lives exclusively in app/ and is ONLY started by app/app.py.
|
| 10 |
+
# NO direct access to fundaments/*, .env, or Guardian (main.py).
|
| 11 |
+
# All config comes from app/.pyfun via app/config.py.
|
| 12 |
+
#
|
| 13 |
+
# MCP SSE transport runs through Quart/hypercorn via /mcp route.
|
| 14 |
+
# All MCP traffic can be intercepted, logged, and transformed in app.py
|
| 15 |
+
# before reaching this handler — this is by design.
|
| 16 |
+
#
|
| 17 |
+
# TOOL REGISTRATION PRINCIPLE:
|
| 18 |
+
# Tools are registered via tools.py — NOT hardcoded here.
|
| 19 |
+
# No key = no provider = no tool = no crash.
|
| 20 |
+
# Server always starts, just with fewer tools.
|
| 21 |
+
# Adding a new tool = update .pyfun + providers.py only. Never touch mcp.py.
|
| 22 |
+
#
|
| 23 |
+
# DEPENDENCY CHAIN (app/* only, no fundaments!):
|
| 24 |
+
# config.py → parses app/.pyfun — single source of truth
|
| 25 |
+
# providers.py → LLM + Search provider registry + fallback chain
|
| 26 |
+
# models.py → model limits, costs, capabilities from .pyfun [MODELS]
|
| 27 |
+
# tools.py → tool registry + execution — reads .pyfun [TOOLS]
|
| 28 |
+
# db_sync.py → internal SQLite IPC (app/* state) — NOT postgresql.py!
|
| 29 |
+
# mcp.py → registers tools only, delegates all logic to tools.py
|
| 30 |
+
# =============================================================================
|
| 31 |
+
import logging
|
| 32 |
+
from typing import Dict, Any
|
| 33 |
+
from . import config as app_config
|
| 34 |
+
from . import providers
|
| 35 |
+
from . import models
|
| 36 |
+
from . import tools
|
| 37 |
+
|
| 38 |
+
logger = logging.getLogger('mcp')
|
| 39 |
+
# =============================================================================
|
| 40 |
+
# Global MCP instance — initialized once via initialize()
|
| 41 |
+
# =============================================================================
|
| 42 |
+
_mcp = None
|
| 43 |
+
# =============================================================================
|
| 44 |
+
# Initialization — called exclusively by app/app.py
|
| 45 |
+
# =============================================================================
|
| 46 |
+
async def initialize() -> None:
|
| 47 |
+
"""
|
| 48 |
+
Initializes the MCP instance and registers all tools.
|
| 49 |
+
Called once by app/app.py during startup sequence.
|
| 50 |
+
No fundaments passed in — fully sandboxed.
|
| 51 |
+
|
| 52 |
+
Registration order:
|
| 53 |
+
1. LLM tools → via tools.py + providers.py (key-gated)
|
| 54 |
+
2. Search tools → via tools.py + providers.py (key-gated)
|
| 55 |
+
3. System tools → always registered, no key required
|
| 56 |
+
4. DB tools → uncomment when db_sync.py is ready
|
| 57 |
+
"""
|
| 58 |
+
global _mcp
|
| 59 |
+
|
| 60 |
+
logger.info("MCP Hub initializing...")
|
| 61 |
+
|
| 62 |
+
hub_cfg = app_config.get_hub()
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
from mcp.server.fastmcp import FastMCP
|
| 66 |
+
except ImportError:
|
| 67 |
+
logger.critical("FastMCP not installed. Run: pip install mcp")
|
| 68 |
+
raise
|
| 69 |
+
|
| 70 |
+
_mcp = FastMCP(
|
| 71 |
+
name=hub_cfg.get("HUB_NAME", "Universal MCP Hub"),
|
| 72 |
+
instructions=(
|
| 73 |
+
f"{hub_cfg.get('HUB_DESCRIPTION', 'Universal MCP Hub on PyFundaments')} "
|
| 74 |
+
"Use list_active_tools to see what is currently available."
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# --- Initialize registries ---
|
| 79 |
+
providers.initialize()
|
| 80 |
+
models.initialize()
|
| 81 |
+
tools.initialize()
|
| 82 |
+
|
| 83 |
+
# --- Register MCP tools ---
|
| 84 |
+
_register_llm_tools(_mcp)
|
| 85 |
+
_register_search_tools(_mcp)
|
| 86 |
+
_register_system_tools(_mcp)
|
| 87 |
+
# _register_db_tools(_mcp) # uncomment when db_sync.py is ready
|
| 88 |
+
|
| 89 |
+
logger.info("MCP Hub initialized.")
|
| 90 |
+
|
| 91 |
+
# =============================================================================
|
| 92 |
+
# Request Handler — Quart /mcp route entry point
|
| 93 |
+
# =============================================================================
|
| 94 |
+
|
| 95 |
+
async def handle_request(request) -> None:
|
| 96 |
+
"""
|
| 97 |
+
Handles incoming MCP SSE requests routed through Quart /mcp endpoint.
|
| 98 |
+
Central interceptor point for all MCP traffic.
|
| 99 |
+
Add auth, logging, rate limiting, payload transformation here as needed.
|
| 100 |
+
"""
|
| 101 |
+
if _mcp is None:
|
| 102 |
+
logger.error("MCP not initialized — call initialize() first.")
|
| 103 |
+
from quart import jsonify
|
| 104 |
+
return jsonify({"error": "MCP not initialized"}), 503
|
| 105 |
+
|
| 106 |
+
# --- Interceptor hooks (uncomment as needed) ---
|
| 107 |
+
# logger.debug(f"MCP request: {request.method} {request.path}")
|
| 108 |
+
# await _check_auth(request)
|
| 109 |
+
# await _rate_limit(request)
|
| 110 |
+
# await _log_payload(request)
|
| 111 |
+
|
| 112 |
+
return await _mcp.handle_sse(request)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# =============================================================================
|
| 116 |
+
# Tool Registration — delegates all logic to tools.py
|
| 117 |
+
# =============================================================================
|
| 118 |
+
|
| 119 |
+
def _register_llm_tools(mcp) -> None:
|
| 120 |
+
"""
|
| 121 |
+
Register LLM completion tool.
|
| 122 |
+
All logic delegated to tools.py → providers.py.
|
| 123 |
+
Adding a new LLM provider = update .pyfun + providers.py. Never touch this.
|
| 124 |
+
"""
|
| 125 |
+
if not providers.list_active_llm():
|
| 126 |
+
logger.info("No active LLM providers — llm_complete tool skipped.")
|
| 127 |
+
return
|
| 128 |
+
|
| 129 |
+
@mcp.tool()
|
| 130 |
+
async def llm_complete(
|
| 131 |
+
prompt: str,
|
| 132 |
+
provider: str = None,
|
| 133 |
+
model: str = None,
|
| 134 |
+
max_tokens: int = 1024,
|
| 135 |
+
) -> str:
|
| 136 |
+
"""
|
| 137 |
+
Send a prompt to any configured LLM provider.
|
| 138 |
+
Automatically follows the fallback chain defined in .pyfun if a provider fails.
|
| 139 |
+
|
| 140 |
+
Args:
|
| 141 |
+
prompt: The input text to send to the model.
|
| 142 |
+
provider: Provider name (e.g. 'anthropic', 'gemini', 'openrouter', 'huggingface').
|
| 143 |
+
Defaults to default_provider from .pyfun [TOOL.llm_complete].
|
| 144 |
+
model: Model name override. Defaults to provider's default_model in .pyfun.
|
| 145 |
+
max_tokens: Maximum tokens in the response. Default: 1024.
|
| 146 |
+
|
| 147 |
+
Returns:
|
| 148 |
+
Model response as plain text string.
|
| 149 |
+
"""
|
| 150 |
+
return await tools.run(
|
| 151 |
+
tool_name="llm_complete",
|
| 152 |
+
prompt=prompt,
|
| 153 |
+
provider_name=provider,
|
| 154 |
+
model=model,
|
| 155 |
+
max_tokens=max_tokens,
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
logger.info(f"Tool registered: llm_complete (active providers: {providers.list_active_llm()})")
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def _register_search_tools(mcp) -> None:
|
| 162 |
+
"""
|
| 163 |
+
Register web search tool.
|
| 164 |
+
All logic delegated to tools.py → providers.py.
|
| 165 |
+
Adding a new search provider = update .pyfun + providers.py. Never touch this.
|
| 166 |
+
"""
|
| 167 |
+
if not providers.list_active_search():
|
| 168 |
+
logger.info("No active search providers — web_search tool skipped.")
|
| 169 |
+
return
|
| 170 |
+
|
| 171 |
+
@mcp.tool()
|
| 172 |
+
async def web_search(
|
| 173 |
+
query: str,
|
| 174 |
+
provider: str = None,
|
| 175 |
+
max_results: int = 5,
|
| 176 |
+
) -> str:
|
| 177 |
+
"""
|
| 178 |
+
Search the web via any configured search provider.
|
| 179 |
+
Automatically follows the fallback chain defined in .pyfun if a provider fails.
|
| 180 |
+
|
| 181 |
+
Args:
|
| 182 |
+
query: Search query string.
|
| 183 |
+
provider: Provider name (e.g. 'brave', 'tavily').
|
| 184 |
+
Defaults to default_provider from .pyfun [TOOL.web_search].
|
| 185 |
+
max_results: Maximum number of results to return. Default: 5.
|
| 186 |
+
|
| 187 |
+
Returns:
|
| 188 |
+
Formatted search results as plain text string.
|
| 189 |
+
"""
|
| 190 |
+
return await tools.run(
|
| 191 |
+
tool_name="web_search",
|
| 192 |
+
prompt=query,
|
| 193 |
+
provider_name=provider,
|
| 194 |
+
max_results=max_results,
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
logger.info(f"Tool registered: web_search (active providers: {providers.list_active_search()})")
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def _register_system_tools(mcp) -> None:
|
| 201 |
+
"""
|
| 202 |
+
System tools — always registered, no ENV key required.
|
| 203 |
+
Exposes hub status and model info without touching secrets.
|
| 204 |
+
"""
|
| 205 |
+
|
| 206 |
+
@mcp.tool()
|
| 207 |
+
def list_active_tools() -> Dict[str, Any]:
|
| 208 |
+
"""
|
| 209 |
+
List all active providers and registered tools.
|
| 210 |
+
Shows ENV key names only — never exposes values or secrets.
|
| 211 |
+
|
| 212 |
+
Returns:
|
| 213 |
+
Dict with hub info, active LLM providers, active search providers,
|
| 214 |
+
available tools and model names.
|
| 215 |
+
"""
|
| 216 |
+
hub = app_config.get_hub()
|
| 217 |
+
return {
|
| 218 |
+
"hub": hub.get("HUB_NAME", "Universal MCP Hub"),
|
| 219 |
+
"version": hub.get("HUB_VERSION", ""),
|
| 220 |
+
"active_llm_providers": providers.list_active_llm(),
|
| 221 |
+
"active_search_providers": providers.list_active_search(),
|
| 222 |
+
"active_tools": tools.list_all(),
|
| 223 |
+
"available_models": models.list_all(),
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
logger.info("Tool registered: list_active_tools")
|
| 227 |
+
|
| 228 |
+
@mcp.tool()
|
| 229 |
+
def health_check() -> Dict[str, str]:
|
| 230 |
+
"""
|
| 231 |
+
Health check endpoint for HuggingFace Spaces and monitoring systems.
|
| 232 |
+
|
| 233 |
+
Returns:
|
| 234 |
+
Dict with service status.
|
| 235 |
+
"""
|
| 236 |
+
return {"status": "ok", "service": "Universal MCP Hub"}
|
| 237 |
+
|
| 238 |
+
logger.info("Tool registered: health_check")
|
| 239 |
+
|
| 240 |
+
@mcp.tool()
|
| 241 |
+
def get_model_info(model_name: str) -> Dict[str, Any]:
|
| 242 |
+
"""
|
| 243 |
+
Get limits, costs, and capabilities for a specific model.
|
| 244 |
+
|
| 245 |
+
Args:
|
| 246 |
+
model_name: Model name as defined in .pyfun [MODELS] (e.g. 'claude-sonnet-4-6').
|
| 247 |
+
|
| 248 |
+
Returns:
|
| 249 |
+
Dict with context size, max output tokens, rate limits, costs, and capabilities.
|
| 250 |
+
Returns empty dict if model is not configured in .pyfun.
|
| 251 |
+
"""
|
| 252 |
+
return models.get(model_name)
|
| 253 |
+
|
| 254 |
+
logger.info("Tool registered: get_model_info")
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
# =============================================================================
|
| 258 |
+
# DB Tools — uncomment when db_sync.py is ready
|
| 259 |
+
# =============================================================================
|
| 260 |
+
|
| 261 |
+
# def _register_db_tools(mcp) -> None:
|
| 262 |
+
# """
|
| 263 |
+
# Register internal SQLite query tool.
|
| 264 |
+
# Uses db_sync.py (app/* internal SQLite) — NOT postgresql.py (Guardian-only)!
|
| 265 |
+
# Only SELECT queries are permitted — read-only by design.
|
| 266 |
+
# """
|
| 267 |
+
# from . import db_sync
|
| 268 |
+
#
|
| 269 |
+
# @mcp.tool()
|
| 270 |
+
# async def db_query(query: str) -> list:
|
| 271 |
+
# """
|
| 272 |
+
# Execute a read-only SELECT query on the internal hub state database.
|
| 273 |
+
# Only SELECT statements are allowed — write operations are blocked.
|
| 274 |
+
#
|
| 275 |
+
# Args:
|
| 276 |
+
# query: SQL SELECT statement to execute.
|
| 277 |
+
#
|
| 278 |
+
# Returns:
|
| 279 |
+
# List of result rows as dicts.
|
| 280 |
+
# """
|
| 281 |
+
# return await db_sync.query(query)
|
| 282 |
+
#
|
| 283 |
+
# logger.info("Tool registered: db_query")
|
| 284 |
+
# =============================================================================
|
| 285 |
+
# Direct execution guard
|
| 286 |
+
# =============================================================================
|
| 287 |
+
|
| 288 |
+
if __name__ == '__main__':
|
| 289 |
+
print("WARNING: Run via main.py → app.py, not directly.")
|
app/mcp_old.py
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# app/mcp.py
|
| 3 |
+
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
|
| 4 |
+
# Copyright 2026 - Volkan Kücükbudak
|
| 5 |
+
# Apache License V. 2 + ESOL 1.1
|
| 6 |
+
# Repo: https://github.com/VolkanSah/Universal-MCP-Hub-sandboxed
|
| 7 |
+
# =============================================================================
|
| 8 |
+
# ARCHITECTURE NOTE:
|
| 9 |
+
# This file lives exclusively in app/ and is ONLY started by app/app.py.
|
| 10 |
+
# NO direct access to fundaments/*, .env, or Guardian (main.py).
|
| 11 |
+
# All config comes from app/.pyfun via app/config.py.
|
| 12 |
+
#
|
| 13 |
+
# MCP SSE transport runs through Quart/hypercorn via /mcp route.
|
| 14 |
+
# All MCP traffic can be intercepted, logged, and transformed in app.py
|
| 15 |
+
# before reaching the MCP handler — this is by design.
|
| 16 |
+
#
|
| 17 |
+
# TOOL REGISTRATION PRINCIPLE:
|
| 18 |
+
# Tools are only registered if their required ENV key exists.
|
| 19 |
+
# No key = no tool = no crash. Server always starts, just with fewer tools.
|
| 20 |
+
# ENV key NAMES come from app/.pyfun — values are never touched here.
|
| 21 |
+
# =============================================================================
|
| 22 |
+
|
| 23 |
+
import asyncio
|
| 24 |
+
import logging
|
| 25 |
+
import os
|
| 26 |
+
from typing import Dict, Any
|
| 27 |
+
|
| 28 |
+
from . import config as app_config # reads app/.pyfun — only config source for app/*
|
| 29 |
+
# from . import polymarket
|
| 30 |
+
|
| 31 |
+
logger = logging.getLogger('mcp')
|
| 32 |
+
|
| 33 |
+
# Global MCP instance — initialized once via initialize()
|
| 34 |
+
_mcp = None
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
async def initialize() -> None:
|
| 38 |
+
"""
|
| 39 |
+
Initializes the MCP instance and registers all tools.
|
| 40 |
+
Called once by app/app.py during startup.
|
| 41 |
+
No fundaments passed in — sandboxed.
|
| 42 |
+
"""
|
| 43 |
+
global _mcp
|
| 44 |
+
|
| 45 |
+
logger.info("MCP Hub initializing...")
|
| 46 |
+
|
| 47 |
+
hub_cfg = app_config.get_hub()
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
from mcp.server.fastmcp import FastMCP
|
| 51 |
+
except ImportError:
|
| 52 |
+
logger.critical("FastMCP not installed. Run: pip install mcp")
|
| 53 |
+
raise
|
| 54 |
+
|
| 55 |
+
_mcp = FastMCP(
|
| 56 |
+
name=hub_cfg.get("HUB_NAME", "Universal MCP Hub"),
|
| 57 |
+
instructions=(
|
| 58 |
+
f"{hub_cfg.get('HUB_DESCRIPTION', 'Universal MCP Hub on PyFundaments')} "
|
| 59 |
+
"Use list_active_tools to see what is currently available."
|
| 60 |
+
)
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# --- Register tools ---
|
| 64 |
+
_register_llm_tools(_mcp)
|
| 65 |
+
_register_search_tools(_mcp)
|
| 66 |
+
# _register_db_tools(_mcp) # uncomment when db_sync is ready
|
| 67 |
+
_register_system_tools(_mcp)
|
| 68 |
+
_register_polymarket_tools(_mcp)
|
| 69 |
+
|
| 70 |
+
logger.info("MCP Hub initialized.")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
async def handle_request(request) -> None:
|
| 74 |
+
"""
|
| 75 |
+
Handles incoming MCP SSE requests routed through Quart /mcp endpoint.
|
| 76 |
+
This is the interceptor point — add auth, logging, rate limiting here.
|
| 77 |
+
"""
|
| 78 |
+
if _mcp is None:
|
| 79 |
+
logger.error("MCP not initialized — call initialize() first.")
|
| 80 |
+
from quart import jsonify
|
| 81 |
+
return jsonify({"error": "MCP not initialized"}), 503
|
| 82 |
+
|
| 83 |
+
# --- Interceptor hooks (add as needed) ---
|
| 84 |
+
# logger.debug(f"MCP request: {request.method} {request.path}")
|
| 85 |
+
# await _check_auth(request)
|
| 86 |
+
# await _rate_limit(request)
|
| 87 |
+
# await _log_payload(request)
|
| 88 |
+
|
| 89 |
+
# --- Forward to FastMCP SSE handler ---
|
| 90 |
+
return await _mcp.handle_sse(request)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# =============================================================================
|
| 94 |
+
# Tool registration helpers
|
| 95 |
+
# =============================================================================
|
| 96 |
+
|
| 97 |
+
def _register_llm_tools(mcp) -> None:
|
| 98 |
+
"""Register LLM tools based on active providers in app/.pyfun + ENV key check."""
|
| 99 |
+
active = app_config.get_active_llm_providers()
|
| 100 |
+
|
| 101 |
+
for name, cfg in active.items():
|
| 102 |
+
env_key = cfg.get("env_key", "")
|
| 103 |
+
if not env_key or not os.getenv(env_key):
|
| 104 |
+
logger.info(f"LLM provider '{name}' skipped — ENV key '{env_key}' not set.")
|
| 105 |
+
continue
|
| 106 |
+
|
| 107 |
+
if name == "anthropic":
|
| 108 |
+
import httpx
|
| 109 |
+
_key = os.getenv(env_key)
|
| 110 |
+
_api_ver = cfg.get("api_version_header", "2023-06-01")
|
| 111 |
+
_base_url = cfg.get("base_url", "https://api.anthropic.com/v1")
|
| 112 |
+
_def_model = cfg.get("default_model", "claude-haiku-4-5-20251001")
|
| 113 |
+
|
| 114 |
+
@mcp.tool()
|
| 115 |
+
async def anthropic_complete(
|
| 116 |
+
prompt: str,
|
| 117 |
+
model: str = _def_model,
|
| 118 |
+
max_tokens: int = 1024
|
| 119 |
+
) -> str:
|
| 120 |
+
"""Send a prompt to Anthropic Claude."""
|
| 121 |
+
async with httpx.AsyncClient() as client:
|
| 122 |
+
r = await client.post(
|
| 123 |
+
f"{_base_url}/messages",
|
| 124 |
+
headers={
|
| 125 |
+
"x-api-key": _key,
|
| 126 |
+
"anthropic-version": _api_ver,
|
| 127 |
+
"content-type": "application/json"
|
| 128 |
+
},
|
| 129 |
+
json={
|
| 130 |
+
"model": model,
|
| 131 |
+
"max_tokens": max_tokens,
|
| 132 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 133 |
+
},
|
| 134 |
+
timeout=60.0
|
| 135 |
+
)
|
| 136 |
+
r.raise_for_status()
|
| 137 |
+
return r.json()["content"][0]["text"]
|
| 138 |
+
|
| 139 |
+
logger.info(f"Tool registered: anthropic_complete (model: {_def_model})")
|
| 140 |
+
|
| 141 |
+
elif name == "gemini":
|
| 142 |
+
import httpx
|
| 143 |
+
_key = os.getenv(env_key)
|
| 144 |
+
_base_url = cfg.get("base_url", "https://generativelanguage.googleapis.com/v1beta")
|
| 145 |
+
_def_model = cfg.get("default_model", "gemini-2.0-flash")
|
| 146 |
+
|
| 147 |
+
@mcp.tool()
|
| 148 |
+
async def gemini_complete(
|
| 149 |
+
prompt: str,
|
| 150 |
+
model: str = _def_model,
|
| 151 |
+
max_tokens: int = 1024
|
| 152 |
+
) -> str:
|
| 153 |
+
"""Send a prompt to Google Gemini."""
|
| 154 |
+
async with httpx.AsyncClient() as client:
|
| 155 |
+
r = await client.post(
|
| 156 |
+
f"{_base_url}/models/{model}:generateContent",
|
| 157 |
+
params={"key": _key},
|
| 158 |
+
json={
|
| 159 |
+
"contents": [{"parts": [{"text": prompt}]}],
|
| 160 |
+
"generationConfig": {"maxOutputTokens": max_tokens}
|
| 161 |
+
},
|
| 162 |
+
timeout=60.0
|
| 163 |
+
)
|
| 164 |
+
r.raise_for_status()
|
| 165 |
+
return r.json()["candidates"][0]["content"]["parts"][0]["text"]
|
| 166 |
+
|
| 167 |
+
logger.info(f"Tool registered: gemini_complete (model: {_def_model})")
|
| 168 |
+
|
| 169 |
+
elif name == "openrouter":
|
| 170 |
+
import httpx
|
| 171 |
+
_key = os.getenv(env_key)
|
| 172 |
+
_base_url = cfg.get("base_url", "https://openrouter.ai/api/v1")
|
| 173 |
+
_def_model = cfg.get("default_model", "mistralai/mistral-7b-instruct")
|
| 174 |
+
_referer = os.getenv("APP_URL", "https://huggingface.co")
|
| 175 |
+
|
| 176 |
+
@mcp.tool()
|
| 177 |
+
async def openrouter_complete(
|
| 178 |
+
prompt: str,
|
| 179 |
+
model: str = _def_model,
|
| 180 |
+
max_tokens: int = 1024
|
| 181 |
+
) -> str:
|
| 182 |
+
"""Send a prompt via OpenRouter (100+ models)."""
|
| 183 |
+
async with httpx.AsyncClient() as client:
|
| 184 |
+
r = await client.post(
|
| 185 |
+
f"{_base_url}/chat/completions",
|
| 186 |
+
headers={
|
| 187 |
+
"Authorization": f"Bearer {_key}",
|
| 188 |
+
"HTTP-Referer": _referer,
|
| 189 |
+
"content-type": "application/json"
|
| 190 |
+
},
|
| 191 |
+
json={
|
| 192 |
+
"model": model,
|
| 193 |
+
"max_tokens": max_tokens,
|
| 194 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 195 |
+
},
|
| 196 |
+
timeout=60.0
|
| 197 |
+
)
|
| 198 |
+
r.raise_for_status()
|
| 199 |
+
return r.json()["choices"][0]["message"]["content"]
|
| 200 |
+
|
| 201 |
+
logger.info(f"Tool registered: openrouter_complete (model: {_def_model})")
|
| 202 |
+
|
| 203 |
+
elif name == "huggingface":
|
| 204 |
+
import httpx
|
| 205 |
+
_key = os.getenv(env_key)
|
| 206 |
+
_base_url = cfg.get("base_url", "https://api-inference.huggingface.co/models")
|
| 207 |
+
_def_model = cfg.get("default_model", "mistralai/Mistral-7B-Instruct-v0.3")
|
| 208 |
+
|
| 209 |
+
@mcp.tool()
|
| 210 |
+
async def hf_inference(
|
| 211 |
+
prompt: str,
|
| 212 |
+
model: str = _def_model,
|
| 213 |
+
max_tokens: int = 512
|
| 214 |
+
) -> str:
|
| 215 |
+
"""Send a prompt to HuggingFace Inference API."""
|
| 216 |
+
async with httpx.AsyncClient() as client:
|
| 217 |
+
r = await client.post(
|
| 218 |
+
f"{_base_url}/{model}/v1/chat/completions",
|
| 219 |
+
headers={
|
| 220 |
+
"Authorization": f"Bearer {_key}",
|
| 221 |
+
"content-type": "application/json"
|
| 222 |
+
},
|
| 223 |
+
json={
|
| 224 |
+
"model": model,
|
| 225 |
+
"max_tokens": max_tokens,
|
| 226 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 227 |
+
},
|
| 228 |
+
timeout=120.0
|
| 229 |
+
)
|
| 230 |
+
r.raise_for_status()
|
| 231 |
+
return r.json()["choices"][0]["message"]["content"]
|
| 232 |
+
|
| 233 |
+
logger.info(f"Tool registered: hf_inference (model: {_def_model})")
|
| 234 |
+
|
| 235 |
+
else:
|
| 236 |
+
logger.info(f"LLM provider '{name}' has no tool handler yet — skipped.")
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def _register_search_tools(mcp) -> None:
|
| 240 |
+
"""Register search tools based on active providers in app/.pyfun + ENV key check."""
|
| 241 |
+
active = app_config.get_active_search_providers()
|
| 242 |
+
|
| 243 |
+
for name, cfg in active.items():
|
| 244 |
+
env_key = cfg.get("env_key", "")
|
| 245 |
+
if not env_key or not os.getenv(env_key):
|
| 246 |
+
logger.info(f"Search provider '{name}' skipped — ENV key '{env_key}' not set.")
|
| 247 |
+
continue
|
| 248 |
+
|
| 249 |
+
if name == "brave":
|
| 250 |
+
import httpx
|
| 251 |
+
_key = os.getenv(env_key)
|
| 252 |
+
_base_url = cfg.get("base_url", "https://api.search.brave.com/res/v1/web/search")
|
| 253 |
+
_def_results = int(cfg.get("default_results", "5"))
|
| 254 |
+
_max_results = int(cfg.get("max_results", "20"))
|
| 255 |
+
|
| 256 |
+
@mcp.tool()
|
| 257 |
+
async def brave_search(query: str, count: int = _def_results) -> str:
|
| 258 |
+
"""Search the web via Brave Search API."""
|
| 259 |
+
async with httpx.AsyncClient() as client:
|
| 260 |
+
r = await client.get(
|
| 261 |
+
_base_url,
|
| 262 |
+
headers={
|
| 263 |
+
"Accept": "application/json",
|
| 264 |
+
"X-Subscription-Token": _key
|
| 265 |
+
},
|
| 266 |
+
params={"q": query, "count": min(count, _max_results)},
|
| 267 |
+
timeout=30.0
|
| 268 |
+
)
|
| 269 |
+
r.raise_for_status()
|
| 270 |
+
results = r.json().get("web", {}).get("results", [])
|
| 271 |
+
if not results:
|
| 272 |
+
return "No results found."
|
| 273 |
+
return "\n\n".join([
|
| 274 |
+
f"{i}. {res.get('title', '')}\n {res.get('url', '')}\n {res.get('description', '')}"
|
| 275 |
+
for i, res in enumerate(results, 1)
|
| 276 |
+
])
|
| 277 |
+
|
| 278 |
+
logger.info("Tool registered: brave_search")
|
| 279 |
+
|
| 280 |
+
elif name == "tavily":
|
| 281 |
+
import httpx
|
| 282 |
+
_key = os.getenv(env_key)
|
| 283 |
+
_base_url = cfg.get("base_url", "https://api.tavily.com/search")
|
| 284 |
+
_def_results = int(cfg.get("default_results", "5"))
|
| 285 |
+
_incl_answer = cfg.get("include_answer", "true").lower() == "true"
|
| 286 |
+
|
| 287 |
+
@mcp.tool()
|
| 288 |
+
async def tavily_search(query: str, max_results: int = _def_results) -> str:
|
| 289 |
+
"""AI-optimized web search via Tavily."""
|
| 290 |
+
async with httpx.AsyncClient() as client:
|
| 291 |
+
r = await client.post(
|
| 292 |
+
_base_url,
|
| 293 |
+
json={
|
| 294 |
+
"api_key": _key,
|
| 295 |
+
"query": query,
|
| 296 |
+
"max_results": max_results,
|
| 297 |
+
"include_answer": _incl_answer
|
| 298 |
+
},
|
| 299 |
+
timeout=30.0
|
| 300 |
+
)
|
| 301 |
+
r.raise_for_status()
|
| 302 |
+
data = r.json()
|
| 303 |
+
parts = []
|
| 304 |
+
if data.get("answer"):
|
| 305 |
+
parts.append(f"Summary: {data['answer']}")
|
| 306 |
+
for res in data.get("results", []):
|
| 307 |
+
parts.append(
|
| 308 |
+
f"- {res['title']}\n {res['url']}\n {res.get('content', '')[:200]}..."
|
| 309 |
+
)
|
| 310 |
+
return "\n\n".join(parts)
|
| 311 |
+
|
| 312 |
+
logger.info("Tool registered: tavily_search")
|
| 313 |
+
|
| 314 |
+
else:
|
| 315 |
+
logger.info(f"Search provider '{name}' has no tool handler yet — skipped.")
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
def _register_system_tools(mcp) -> None:
|
| 319 |
+
"""System tools — always registered, no ENV key required."""
|
| 320 |
+
|
| 321 |
+
@mcp.tool()
|
| 322 |
+
def list_active_tools() -> Dict[str, Any]:
|
| 323 |
+
"""Show active providers and configured integrations (key names only, never values)."""
|
| 324 |
+
llm = app_config.get_active_llm_providers()
|
| 325 |
+
search = app_config.get_active_search_providers()
|
| 326 |
+
hub = app_config.get_hub()
|
| 327 |
+
return {
|
| 328 |
+
"hub": hub.get("HUB_NAME", "Universal MCP Hub"),
|
| 329 |
+
"version": hub.get("HUB_VERSION", ""),
|
| 330 |
+
"active_llm_providers": [n for n, c in llm.items() if os.getenv(c.get("env_key", ""))],
|
| 331 |
+
"active_search_providers":[n for n, c in search.items() if os.getenv(c.get("env_key", ""))],
|
| 332 |
+
}
|
| 333 |
+
logger.info("Tool registered: list_active_tools")
|
| 334 |
+
|
| 335 |
+
@mcp.tool()
|
| 336 |
+
def health_check() -> Dict[str, str]:
|
| 337 |
+
"""Health check for monitoring and HuggingFace Spaces."""
|
| 338 |
+
return {"status": "ok", "service": "Universal MCP Hub"}
|
| 339 |
+
logger.info("Tool registered: health_check")
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
# 3. Neue Funktion — analog zu _register_search_tools():
|
| 344 |
+
def _register_polymarket_tools(mcp) -> None:
|
| 345 |
+
"""Polymarket tools — no ENV key needed, Gamma API is public."""
|
| 346 |
+
|
| 347 |
+
@mcp.tool()
|
| 348 |
+
async def get_markets(category: str = None, limit: int = 20) -> list:
|
| 349 |
+
"""Get active prediction markets, optional category filter."""
|
| 350 |
+
return await polymarket.get_markets(category=category, limit=limit)
|
| 351 |
+
|
| 352 |
+
@mcp.tool()
|
| 353 |
+
async def trending_markets(limit: int = 10) -> list:
|
| 354 |
+
"""Get top trending markets by trading volume."""
|
| 355 |
+
return await polymarket.trending_markets(limit=limit)
|
| 356 |
+
|
| 357 |
+
@mcp.tool()
|
| 358 |
+
async def analyze_market(market_id: str) -> dict:
|
| 359 |
+
"""LLM analysis of a single market. Fallback if no LLM key set."""
|
| 360 |
+
return await polymarket.analyze_market(market_id)
|
| 361 |
+
|
| 362 |
+
@mcp.tool()
|
| 363 |
+
async def summary_report(category: str = None) -> dict:
|
| 364 |
+
"""Summary report for a category or all markets."""
|
| 365 |
+
return await polymarket.summary_report(category=category)
|
| 366 |
+
|
| 367 |
+
@mcp.tool()
|
| 368 |
+
async def polymarket_cache_info() -> dict:
|
| 369 |
+
"""Cache status, available categories, LLM availability."""
|
| 370 |
+
return await polymarket.get_cache_info()
|
| 371 |
+
|
| 372 |
+
logger.info("Tools registered: polymarket (5 tools)")
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
# =============================================================================
|
| 376 |
+
# Direct execution guard
|
| 377 |
+
# =============================================================================
|
| 378 |
+
if __name__ == '__main__':
|
| 379 |
+
print("WARNING: Run via main.py, not directly.")
|
app/models.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# # app/models.py
|
| 3 |
+
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
|
| 4 |
+
# Copyright 2026 - Volkan Kücükbudak
|
| 5 |
+
# Apache License V. 2 + ESOL 1.1
|
| 6 |
+
# Repo: https://github.com/VolkanSah/Universal-MCP-Hub-sandboxed
|
| 7 |
+
# =============================================================================
|
| 8 |
+
# ARCHITECTURE NOTE:
|
| 9 |
+
# This file lives exclusively in app/ and is ONLY started by app/app.py.
|
| 10 |
+
# NO direct access to fundaments/*, .env, or Guardian (main.py).
|
| 11 |
+
# All config comes from app/.pyfun via app/config.py.
|
| 12 |
+
#
|
| 13 |
+
#
|
| 14 |
+
# TOOL REGISTRATION PRINCIPLE:
|
| 15 |
+
# Tools are registered via models.py and models.py .
|
| 16 |
+
# No key = no provider = no tool = no crash.
|
| 17 |
+
# Adding a new provider = update .pyfun + providers.py only. Never touch mcp.py!
|
| 18 |
+
#
|
| 19 |
+
# DEPENDENCY CHAIN (app/* only, no fundaments!):
|
| 20 |
+
# config.py → parses app/.pyfun — single source of truth
|
| 21 |
+
# providers.py → LLM + Search provider registry + fallback chain
|
| 22 |
+
# models.py → model limits, costs, capabilities from .pyfun [MODELS]
|
| 23 |
+
# db_sync.py → internal SQLite IPC (app/* state) — NOT postgresql.py!
|
| 24 |
+
# mcp.py → registers tools only, delegates all logic to providers/*
|
| 25 |
+
# =============================================================================
|
| 26 |
+
# app/models.py
|
| 27 |
+
from . import config
|
| 28 |
+
import logging
|
| 29 |
+
|
| 30 |
+
logger = logging.getLogger("models")
|
| 31 |
+
|
| 32 |
+
_registry: dict = {}
|
| 33 |
+
|
| 34 |
+
def initialize() -> None:
|
| 35 |
+
"""Build model registry from .pyfun [MODELS]"""
|
| 36 |
+
global _registry
|
| 37 |
+
_registry = config.get_models()
|
| 38 |
+
logger.info(f"Models loaded: {list(_registry.keys())}")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def get(model_name: str) -> dict:
|
| 42 |
+
"""Get model config by name."""
|
| 43 |
+
return _registry.get(model_name, {})
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def get_limit(model_name: str, key: str, default=None):
|
| 47 |
+
"""Get specific limit for a model."""
|
| 48 |
+
return _registry.get(model_name, {}).get(key, default)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def for_provider(provider_name: str) -> dict:
|
| 52 |
+
"""Get all models for a provider."""
|
| 53 |
+
return config.get_models_for_provider(provider_name)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def max_tokens(model_name: str) -> int:
|
| 57 |
+
return int(get_limit(model_name, "max_output_tokens", "1024"))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def context_size(model_name: str) -> int:
|
| 61 |
+
return int(get_limit(model_name, "context_tokens", "4096"))
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def cost_input(model_name: str) -> float:
|
| 65 |
+
return float(get_limit(model_name, "cost_input_per_1k", "0"))
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def cost_output(model_name: str) -> float:
|
| 69 |
+
return float(get_limit(model_name, "cost_output_per_1k", "0"))
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def list_all() -> list:
|
| 73 |
+
return list(_registry.keys())
|
app/provider.py
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# # app/providers.py
|
| 3 |
+
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
|
| 4 |
+
# Copyright 2026 - Volkan Kücükbudak
|
| 5 |
+
# Apache License V. 2 + ESOL 1.1
|
| 6 |
+
# Repo: https://github.com/VolkanSah/Universal-MCP-Hub-sandboxed
|
| 7 |
+
# =============================================================================
|
| 8 |
+
# ARCHITECTURE NOTE:
|
| 9 |
+
# This file lives exclusively in app/ and is ONLY started by app/app.py.
|
| 10 |
+
# NO direct access to fundaments/*, .env, or Guardian (main.py).
|
| 11 |
+
# All config comes from app/.pyfun via app/config.py.
|
| 12 |
+
#
|
| 13 |
+
#
|
| 14 |
+
# TOOL REGISTRATION PRINCIPLE:
|
| 15 |
+
# Tools are registered via providers.py and models.py .
|
| 16 |
+
# No key = no provider = no tool = no crash.
|
| 17 |
+
# Adding a new provider = update .pyfun + providers.py only. Never touch mcp.py!
|
| 18 |
+
#
|
| 19 |
+
# DEPENDENCY CHAIN (app/* only, no fundaments!):
|
| 20 |
+
# config.py → parses app/.pyfun — single source of truth
|
| 21 |
+
# providers.py → LLM + Search provider registry + fallback chain
|
| 22 |
+
# models.py → model limits, costs, capabilities from .pyfun [MODELS]
|
| 23 |
+
# db_sync.py → internal SQLite IPC (app/* state) — NOT postgresql.py!
|
| 24 |
+
# mcp.py → registers tools only, delegates all logic to providers/*
|
| 25 |
+
# =============================================================================
|
| 26 |
+
|
| 27 |
+
from . import config
|
| 28 |
+
import os
|
| 29 |
+
import httpx
|
| 30 |
+
import logging
|
| 31 |
+
|
| 32 |
+
logger = logging.getLogger("providers")
|
| 33 |
+
|
| 34 |
+
# =============================================================================
|
| 35 |
+
# Base Provider — gemeinsame Logic EINMAL
|
| 36 |
+
# =============================================================================
|
| 37 |
+
class BaseProvider:
|
| 38 |
+
def __init__(self, name: str, cfg: dict):
|
| 39 |
+
self.name = name
|
| 40 |
+
self.key = os.getenv(cfg.get("env_key", ""))
|
| 41 |
+
self.base_url = cfg.get("base_url", "")
|
| 42 |
+
self.fallback = cfg.get("fallback_to", "")
|
| 43 |
+
self.timeout = int(config.get_limits().get("REQUEST_TIMEOUT_SEC", "60"))
|
| 44 |
+
self.model = cfg.get("default_model", "")
|
| 45 |
+
|
| 46 |
+
async def complete(self, prompt: str, model: str, max_tokens: int) -> str:
|
| 47 |
+
raise NotImplementedError
|
| 48 |
+
|
| 49 |
+
async def _post(self, url: str, headers: dict, payload: dict) -> dict:
|
| 50 |
+
"""EINMAL — alle Provider nutzen das!"""
|
| 51 |
+
async with httpx.AsyncClient() as client:
|
| 52 |
+
r = await client.post(
|
| 53 |
+
url,
|
| 54 |
+
headers=headers,
|
| 55 |
+
json=payload,
|
| 56 |
+
timeout=self.timeout
|
| 57 |
+
)
|
| 58 |
+
r.raise_for_status()
|
| 59 |
+
return r.json()
|
| 60 |
+
|
| 61 |
+
# =============================================================================
|
| 62 |
+
# Provider Implementierungen — nur parse logic verschieden
|
| 63 |
+
# =============================================================================
|
| 64 |
+
class AnthropicProvider(BaseProvider):
|
| 65 |
+
async def complete(self, prompt: str, model: str = None, max_tokens: int = 1024) -> str:
|
| 66 |
+
cfg = config.get_active_llm_providers().get("anthropic", {})
|
| 67 |
+
data = await self._post(
|
| 68 |
+
f"{self.base_url}/messages",
|
| 69 |
+
headers={
|
| 70 |
+
"x-api-key": self.key,
|
| 71 |
+
"anthropic-version": cfg.get("api_version_header", "2023-06-01"),
|
| 72 |
+
"content-type": "application/json",
|
| 73 |
+
},
|
| 74 |
+
payload={
|
| 75 |
+
"model": model or self.model,
|
| 76 |
+
"max_tokens": max_tokens,
|
| 77 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 78 |
+
}
|
| 79 |
+
)
|
| 80 |
+
return data["content"][0]["text"]
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
class GeminiProvider(BaseProvider):
|
| 84 |
+
async def complete(self, prompt: str, model: str = None, max_tokens: int = 1024) -> str:
|
| 85 |
+
m = model or self.model
|
| 86 |
+
async with httpx.AsyncClient() as client:
|
| 87 |
+
r = await client.post(
|
| 88 |
+
f"{self.base_url}/models/{m}:generateContent",
|
| 89 |
+
params={"key": self.key},
|
| 90 |
+
json={
|
| 91 |
+
"contents": [{"parts": [{"text": prompt}]}],
|
| 92 |
+
"generationConfig":{"maxOutputTokens": max_tokens},
|
| 93 |
+
},
|
| 94 |
+
timeout=self.timeout
|
| 95 |
+
)
|
| 96 |
+
r.raise_for_status()
|
| 97 |
+
return r.json()["candidates"][0]["content"]["parts"][0]["text"]
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class OpenRouterProvider(BaseProvider):
|
| 101 |
+
async def complete(self, prompt: str, model: str = None, max_tokens: int = 1024) -> str:
|
| 102 |
+
data = await self._post(
|
| 103 |
+
f"{self.base_url}/chat/completions",
|
| 104 |
+
headers={
|
| 105 |
+
"Authorization": f"Bearer {self.key}",
|
| 106 |
+
"HTTP-Referer": os.getenv("APP_URL", "https://huggingface.co"),
|
| 107 |
+
"content-type": "application/json",
|
| 108 |
+
},
|
| 109 |
+
payload={
|
| 110 |
+
"model": model or self.model,
|
| 111 |
+
"max_tokens": max_tokens,
|
| 112 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 113 |
+
}
|
| 114 |
+
)
|
| 115 |
+
return data["choices"][0]["message"]["content"]
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class HuggingFaceProvider(BaseProvider):
|
| 119 |
+
async def complete(self, prompt: str, model: str = None, max_tokens: int = 512) -> str:
|
| 120 |
+
m = model or self.model
|
| 121 |
+
data = await self._post(
|
| 122 |
+
f"{self.base_url}/{m}/v1/chat/completions",
|
| 123 |
+
headers={
|
| 124 |
+
"Authorization": f"Bearer {self.key}",
|
| 125 |
+
"content-type": "application/json",
|
| 126 |
+
},
|
| 127 |
+
payload={
|
| 128 |
+
"model": m,
|
| 129 |
+
"max_tokens": max_tokens,
|
| 130 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 131 |
+
}
|
| 132 |
+
)
|
| 133 |
+
return data["choices"][0]["message"]["content"]
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
# =============================================================================
|
| 137 |
+
# Provider Registry — gebaut aus .pyfun
|
| 138 |
+
# =============================================================================
|
| 139 |
+
_PROVIDER_CLASSES = {
|
| 140 |
+
"anthropic": AnthropicProvider,
|
| 141 |
+
"gemini": GeminiProvider,
|
| 142 |
+
"openrouter": OpenRouterProvider,
|
| 143 |
+
"huggingface": HuggingFaceProvider,
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
_registry: dict = {}
|
| 147 |
+
|
| 148 |
+
def initialize() -> None:
|
| 149 |
+
"""Build provider registry from .pyfun — called by app.py"""
|
| 150 |
+
global _registry
|
| 151 |
+
active = config.get_active_llm_providers()
|
| 152 |
+
|
| 153 |
+
for name, cfg in active.items():
|
| 154 |
+
env_key = cfg.get("env_key", "")
|
| 155 |
+
if not env_key or not os.getenv(env_key):
|
| 156 |
+
logger.info(f"Provider '{name}' skipped — ENV key not set.")
|
| 157 |
+
continue
|
| 158 |
+
cls = _PROVIDER_CLASSES.get(name)
|
| 159 |
+
if not cls:
|
| 160 |
+
logger.info(f"Provider '{name}' has no handler yet — skipped.")
|
| 161 |
+
continue
|
| 162 |
+
_registry[name] = cls(name, cfg)
|
| 163 |
+
logger.info(f"Provider registered: {name}")
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
async def complete(
|
| 167 |
+
prompt: str,
|
| 168 |
+
provider_name: str = None,
|
| 169 |
+
model: str = None,
|
| 170 |
+
max_tokens: int = 1024
|
| 171 |
+
) -> str:
|
| 172 |
+
"""
|
| 173 |
+
Complete with fallback chain from .pyfun.
|
| 174 |
+
anthropic → fails → openrouter → fails → error
|
| 175 |
+
"""
|
| 176 |
+
# default provider aus [TOOL.llm_complete] → default_provider
|
| 177 |
+
if not provider_name:
|
| 178 |
+
tools = config.get_active_tools()
|
| 179 |
+
provider_name = tools.get("llm_complete", {}).get("default_provider", "anthropic")
|
| 180 |
+
|
| 181 |
+
visited = set()
|
| 182 |
+
current = provider_name
|
| 183 |
+
|
| 184 |
+
while current and current not in visited:
|
| 185 |
+
visited.add(current)
|
| 186 |
+
provider = _registry.get(current)
|
| 187 |
+
|
| 188 |
+
if not provider:
|
| 189 |
+
logger.warning(f"Provider '{current}' not in registry — trying fallback.")
|
| 190 |
+
else:
|
| 191 |
+
try:
|
| 192 |
+
return await provider.complete(prompt, model, max_tokens)
|
| 193 |
+
except Exception as e:
|
| 194 |
+
logger.warning(f"Provider '{current}' failed: {e} — trying fallback.")
|
| 195 |
+
|
| 196 |
+
# Fallback aus .pyfun
|
| 197 |
+
cfg = config.get_active_llm_providers().get(current, {})
|
| 198 |
+
current = cfg.get("fallback_to", "")
|
| 199 |
+
|
| 200 |
+
raise RuntimeError("All providers failed — no fallback available.")
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def get(name: str) -> BaseProvider:
|
| 204 |
+
"""Get a specific provider by name."""
|
| 205 |
+
return _registry.get(name)
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def list_active() -> list:
|
| 209 |
+
"""List all active provider names."""
|
| 210 |
+
return list(_registry.keys())
|
app/tmp/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
> DO NOT DELETE THIS FOLDER!
|
app/tools.py
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# app/tools.py
|
| 3 |
+
# Tool Registry — Modular Wrapper
|
| 4 |
+
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
|
| 5 |
+
# Copyright 2026 - Volkan Kücükbudak
|
| 6 |
+
# Apache License V. 2 + ESOL 1.1
|
| 7 |
+
# Repo: https://github.com/VolkanSah/Universal-MCP-Hub-sandboxed
|
| 8 |
+
# =============================================================================
|
| 9 |
+
# ARCHITECTURE NOTE:
|
| 10 |
+
# This file lives exclusively in app/ and is ONLY started by app/app.py.
|
| 11 |
+
# NO direct access to fundaments/*, .env, or Guardian (main.py).
|
| 12 |
+
# All config comes from app/.pyfun via app/config.py.
|
| 13 |
+
#
|
| 14 |
+
# TOOL REGISTRY PRINCIPLE:
|
| 15 |
+
# Tools are defined in .pyfun [TOOLS] — never hardcoded here.
|
| 16 |
+
# Adding a new tool = update .pyfun only. Never touch this file.
|
| 17 |
+
# config.py parses [TOOLS] and delivers ready-to-use dicts.
|
| 18 |
+
#
|
| 19 |
+
# DEPENDENCY CHAIN:
|
| 20 |
+
# .pyfun → config.py → tools.py → mcp.py
|
| 21 |
+
# tools.py delegates execution to providers.py — never calls APIs directly.
|
| 22 |
+
# =============================================================================
|
| 23 |
+
|
| 24 |
+
import logging
|
| 25 |
+
import os
|
| 26 |
+
from typing import Any, Dict, Optional
|
| 27 |
+
|
| 28 |
+
from . import config # reads app/.pyfun — single source of truth
|
| 29 |
+
from . import providers # LLM + Search execution + fallback chain
|
| 30 |
+
|
| 31 |
+
logger = logging.getLogger("tools")
|
| 32 |
+
|
| 33 |
+
# =============================================================================
|
| 34 |
+
# Internal Registry — built from .pyfun [TOOLS] at initialize()
|
| 35 |
+
# =============================================================================
|
| 36 |
+
_registry: Dict[str, Dict] = {}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# =============================================================================
|
| 40 |
+
# Initialization — called by app/app.py (parameterless, sandboxed)
|
| 41 |
+
# =============================================================================
|
| 42 |
+
|
| 43 |
+
def initialize() -> None:
|
| 44 |
+
"""
|
| 45 |
+
Builds the tool registry from .pyfun [TOOLS].
|
| 46 |
+
Called once by app/app.py during startup sequence.
|
| 47 |
+
No fundaments passed in — fully sandboxed.
|
| 48 |
+
|
| 49 |
+
Loads all active tools and their config (description, provider_type,
|
| 50 |
+
default_provider, timeout_sec, system_prompt, etc.) into _registry.
|
| 51 |
+
Inactive tools (active = "false") are skipped silently.
|
| 52 |
+
"""
|
| 53 |
+
global _registry
|
| 54 |
+
_registry = config.get_active_tools()
|
| 55 |
+
logger.info(f"Tools loaded: {list(_registry.keys())}")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# =============================================================================
|
| 59 |
+
# Public API — used by mcp.py tool handlers
|
| 60 |
+
# =============================================================================
|
| 61 |
+
|
| 62 |
+
async def run(
|
| 63 |
+
tool_name: str,
|
| 64 |
+
prompt: str,
|
| 65 |
+
provider_name: Optional[str] = None,
|
| 66 |
+
model: Optional[str] = None,
|
| 67 |
+
max_tokens: int = 1024,
|
| 68 |
+
) -> str:
|
| 69 |
+
"""
|
| 70 |
+
Execute a tool by name.
|
| 71 |
+
Reads tool config from registry, delegates to providers.py.
|
| 72 |
+
Applies system_prompt from .pyfun if defined.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
tool_name: Tool name as defined in .pyfun [TOOLS] (e.g. 'llm_complete').
|
| 76 |
+
prompt: User input / query string.
|
| 77 |
+
provider_name: Override provider. Defaults to tool's default_provider in .pyfun.
|
| 78 |
+
model: Override model. Defaults to provider's default_model in .pyfun.
|
| 79 |
+
max_tokens: Max tokens for LLM response. Default: 1024.
|
| 80 |
+
|
| 81 |
+
Returns:
|
| 82 |
+
Tool response as plain text string.
|
| 83 |
+
|
| 84 |
+
Raises:
|
| 85 |
+
ValueError: If tool_name is not found in registry.
|
| 86 |
+
RuntimeError: If all providers fail (propagated from providers.py).
|
| 87 |
+
"""
|
| 88 |
+
tool_cfg = _registry.get(tool_name)
|
| 89 |
+
if not tool_cfg:
|
| 90 |
+
raise ValueError(f"Tool '{tool_name}' not found in registry or not active.")
|
| 91 |
+
|
| 92 |
+
provider_type = tool_cfg.get("provider_type", "llm")
|
| 93 |
+
default_provider = provider_name or tool_cfg.get("default_provider", "")
|
| 94 |
+
system_prompt = tool_cfg.get("system_prompt", "")
|
| 95 |
+
|
| 96 |
+
# Build full prompt — prepend system_prompt if defined in .pyfun
|
| 97 |
+
full_prompt = f"{system_prompt}\n\n{prompt}".strip() if system_prompt else prompt
|
| 98 |
+
|
| 99 |
+
# --- LLM tools ---
|
| 100 |
+
if provider_type == "llm":
|
| 101 |
+
return await providers.llm_complete(
|
| 102 |
+
prompt=full_prompt,
|
| 103 |
+
provider_name=default_provider,
|
| 104 |
+
model=model,
|
| 105 |
+
max_tokens=max_tokens,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# --- Search tools ---
|
| 109 |
+
if provider_type == "search":
|
| 110 |
+
return await providers.search(
|
| 111 |
+
query=prompt,
|
| 112 |
+
provider_name=default_provider,
|
| 113 |
+
max_results=int(tool_cfg.get("default_results", "5")),
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# --- DB tools (read-only, delegated to db_sync when ready) ---
|
| 117 |
+
if provider_type == "db":
|
| 118 |
+
# db_sync not yet implemented — return informative message
|
| 119 |
+
logger.info("db_query tool called — db_sync.py not yet active.")
|
| 120 |
+
return "Database query tool is not yet active. Configure db_sync.py first."
|
| 121 |
+
|
| 122 |
+
# --- Unknown provider type ---
|
| 123 |
+
logger.warning(f"Tool '{tool_name}' has unknown provider_type '{provider_type}' — skipped.")
|
| 124 |
+
return f"Tool '{tool_name}' provider type '{provider_type}' is not yet implemented."
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
# =============================================================================
|
| 128 |
+
# Registry helpers — used by mcp.py and system tools
|
| 129 |
+
# =============================================================================
|
| 130 |
+
|
| 131 |
+
def get(tool_name: str) -> Dict[str, Any]:
|
| 132 |
+
"""
|
| 133 |
+
Get full config dict for a tool.
|
| 134 |
+
|
| 135 |
+
Args:
|
| 136 |
+
tool_name: Tool name as defined in .pyfun [TOOLS].
|
| 137 |
+
|
| 138 |
+
Returns:
|
| 139 |
+
Tool config dict, or empty dict if not found.
|
| 140 |
+
"""
|
| 141 |
+
return _registry.get(tool_name, {})
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def get_description(tool_name: str) -> str:
|
| 145 |
+
"""
|
| 146 |
+
Get the description of a tool (from .pyfun).
|
| 147 |
+
|
| 148 |
+
Args:
|
| 149 |
+
tool_name: Tool name as defined in .pyfun [TOOLS].
|
| 150 |
+
|
| 151 |
+
Returns:
|
| 152 |
+
Description string, or empty string if not found.
|
| 153 |
+
"""
|
| 154 |
+
return _registry.get(tool_name, {}).get("description", "")
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def get_system_prompt(tool_name: str) -> str:
|
| 158 |
+
"""
|
| 159 |
+
Get the system_prompt of a tool (from .pyfun).
|
| 160 |
+
Returns empty string if no system_prompt is defined.
|
| 161 |
+
|
| 162 |
+
Args:
|
| 163 |
+
tool_name: Tool name as defined in .pyfun [TOOLS].
|
| 164 |
+
|
| 165 |
+
Returns:
|
| 166 |
+
System prompt string, or empty string if not configured.
|
| 167 |
+
"""
|
| 168 |
+
return _registry.get(tool_name, {}).get("system_prompt", "")
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def get_timeout(tool_name: str) -> int:
|
| 172 |
+
"""
|
| 173 |
+
Get the timeout in seconds for a tool (from .pyfun).
|
| 174 |
+
|
| 175 |
+
Args:
|
| 176 |
+
tool_name: Tool name as defined in .pyfun [TOOLS].
|
| 177 |
+
|
| 178 |
+
Returns:
|
| 179 |
+
Timeout in seconds (int). Defaults to 60 if not configured.
|
| 180 |
+
"""
|
| 181 |
+
return int(_registry.get(tool_name, {}).get("timeout_sec", "60"))
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def get_provider_type(tool_name: str) -> str:
|
| 185 |
+
"""
|
| 186 |
+
Get the provider_type of a tool (llm | search | db | image | sandbox).
|
| 187 |
+
|
| 188 |
+
Args:
|
| 189 |
+
tool_name: Tool name as defined in .pyfun [TOOLS].
|
| 190 |
+
|
| 191 |
+
Returns:
|
| 192 |
+
Provider type string, or empty string if not found.
|
| 193 |
+
"""
|
| 194 |
+
return _registry.get(tool_name, {}).get("provider_type", "")
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def list_all() -> list:
|
| 198 |
+
"""
|
| 199 |
+
List all active tool names from registry.
|
| 200 |
+
|
| 201 |
+
Returns:
|
| 202 |
+
List of active tool name strings.
|
| 203 |
+
"""
|
| 204 |
+
return list(_registry.keys())
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def list_by_type(provider_type: str) -> list:
|
| 208 |
+
"""
|
| 209 |
+
List all active tools of a specific provider_type.
|
| 210 |
+
|
| 211 |
+
Args:
|
| 212 |
+
provider_type: e.g. 'llm', 'search', 'db', 'image', 'sandbox'.
|
| 213 |
+
|
| 214 |
+
Returns:
|
| 215 |
+
List of tool name strings matching the provider_type.
|
| 216 |
+
"""
|
| 217 |
+
return [
|
| 218 |
+
name for name, cfg in _registry.items()
|
| 219 |
+
if cfg.get("provider_type", "") == provider_type
|
| 220 |
+
]
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
# =============================================================================
|
| 224 |
+
# Direct execution guard
|
| 225 |
+
# =============================================================================
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
print("WARNING: Run via main.py → app.py, not directly.")
|
app/web/tmp/__init__py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|
app/web/tools/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
> where will be tmp + do_work for webhooks
|
app/web/tools/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|