Skip to content

Commit 0b8b67e

Browse files
fix(mcp): address PR review comments
- Use global mcp instance with add_middleware() instead of creating new instance (fixes tool registration) - Add error handling for middleware creation with logging.exception - Use Python 3.10+ lowercase dict type hints instead of Dict - Fix multi-line docstring format with summary on first line - Add missing trailing commas for better code style - Improve middleware creation efficiency with try-except blocks
1 parent a0bd5de commit 0b8b67e

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

superset/mcp_service/mcp_config.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import logging
2020
import secrets
21-
from typing import Any, Dict, Optional
21+
from typing import Any
2222

2323
from flask import Flask
2424

@@ -34,7 +34,7 @@
3434
WEBDRIVER_BASEURL_USER_FRIENDLY = WEBDRIVER_BASEURL
3535

3636
# Feature flags for MCP
37-
MCP_FEATURE_FLAGS: Dict[str, Any] = {
37+
MCP_FEATURE_FLAGS: dict[str, Any] = {
3838
"MCP_SERVICE": True,
3939
}
4040

@@ -106,7 +106,7 @@
106106
}
107107

108108

109-
def create_default_mcp_auth_factory(app: Flask) -> Optional[Any]:
109+
def create_default_mcp_auth_factory(app: Flask) -> Any | None:
110110
"""Default MCP auth factory using app.config values."""
111111
if not app.config.get("MCP_AUTH_ENABLED", False):
112112
return None
@@ -154,7 +154,7 @@ def create_default_mcp_auth_factory(app: Flask) -> Optional[Any]:
154154
return None
155155

156156

157-
def default_user_resolver(app: Any, access_token: Any) -> Optional[str]:
157+
def default_user_resolver(app: Any, access_token: Any) -> str | None:
158158
"""Extract username from JWT token claims."""
159159
logger.info(
160160
"Resolving user from token: type=%s, token=%s",
@@ -179,7 +179,7 @@ def generate_secret_key() -> str:
179179
return secrets.token_urlsafe(42)
180180

181181

182-
def get_mcp_config(app_config: Dict[str, Any] | None = None) -> Dict[str, Any]:
182+
def get_mcp_config(app_config: dict[str, Any] | None = None) -> dict[str, Any]:
183183
"""
184184
Get complete MCP configuration dictionary.
185185
@@ -207,8 +207,8 @@ def get_mcp_config(app_config: Dict[str, Any] | None = None) -> Dict[str, Any]:
207207

208208

209209
def get_mcp_config_with_overrides(
210-
app_config: Dict[str, Any] | None = None,
211-
) -> Dict[str, Any]:
210+
app_config: dict[str, Any] | None = None,
211+
) -> dict[str, Any]:
212212
"""
213213
Alternative approach: Allow any app_config keys, not just predefined ones.
214214
@@ -223,16 +223,16 @@ def get_mcp_config_with_overrides(
223223

224224

225225
def create_response_caching_middleware(
226-
app_config: Dict[str, Any] | None = None,
227-
) -> Optional[Any]:
228-
"""
229-
Create ResponseCachingMiddleware with configuration from app.config.
226+
app_config: dict[str, Any] | None = None,
227+
) -> Any | None:
228+
"""Create ResponseCachingMiddleware with configuration from app.config.
230229
231230
Args:
232231
app_config: Optional Flask app configuration dict
233232
234233
Returns:
235234
ResponseCachingMiddleware instance or None if caching is disabled
235+
236236
"""
237237
app_config = app_config or {}
238238

@@ -269,7 +269,7 @@ def create_response_caching_middleware(
269269
}
270270

271271
# Tool caching with include/exclude lists
272-
call_tool_settings: Dict[str, Any] = {
272+
call_tool_settings: dict[str, Any] = {
273273
"ttl": cache_config.get("call_tool_ttl", 3600),
274274
"enabled": True,
275275
}
@@ -298,7 +298,8 @@ def create_response_caching_middleware(
298298

299299
if cache_config.get("excluded_tools"):
300300
logger.info(
301-
"Excluded tools from caching: %s", cache_config["excluded_tools"]
301+
"Excluded tools from caching: %s",
302+
cache_config["excluded_tools"],
302303
)
303304

304305
return middleware
@@ -309,11 +310,11 @@ def create_response_caching_middleware(
309310
)
310311
return None
311312
except Exception as e:
312-
logger.error("Failed to create response caching middleware: %s", e)
313+
logger.exception("Failed to create response caching middleware: %s", e)
313314
return None
314315

315316

316-
def get_mcp_factory_config() -> Dict[str, Any]:
317+
def get_mcp_factory_config() -> dict[str, Any]:
317318
"""
318319
Get FastMCP factory configuration.
319320

superset/mcp_service/server.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,24 @@ def run_server(
8282
factory_config = get_mcp_factory_config()
8383
mcp_instance = create_mcp_app(**factory_config)
8484
else:
85-
# Use default initialization with response caching middleware
86-
logging.info("Creating MCP app with default configuration...")
85+
# Use default global mcp instance with middleware
86+
# This ensures all registered tools are available
87+
logging.info("Using global MCP instance with middleware...")
8788

88-
# Import here to avoid circular imports
89+
# Import global mcp instance and middleware config
90+
from superset.mcp_service.app import mcp
8991
from superset.mcp_service.mcp_config import create_response_caching_middleware
9092

91-
# Create middleware list - response caching is enabled by default
92-
middleware_list = []
93-
caching_middleware = create_response_caching_middleware()
94-
if caching_middleware:
95-
middleware_list.append(caching_middleware)
93+
# Add response caching middleware if available
94+
try:
95+
caching_middleware = create_response_caching_middleware()
96+
if caching_middleware is not None:
97+
mcp.add_middleware(caching_middleware)
98+
logging.info("Response caching middleware added successfully")
99+
except Exception as e:
100+
logging.warning("Failed to add response caching middleware: %s", e)
96101

97-
# Create MCP instance with middleware
98-
mcp_instance = create_mcp_app(middleware=middleware_list or None)
102+
mcp_instance = mcp
99103

100104
env_key = f"FASTMCP_RUNNING_{port}"
101105
if not os.environ.get(env_key):

0 commit comments

Comments
 (0)