Skip to content

Commit 6bb4feb

Browse files
refactor(mcp): improve config loading to read from app.config first
Update MCP config loading to properly read from Flask app.config before falling back to defaults. This allows users to override MCP settings in their superset_config.py file. - Refactor get_mcp_config() to accept app_config parameter - Use Pythonic dict unpacking for cleaner config merging - Add type hints with modern union syntax - Update flask_singleton.py to pass app.config to get_mcp_config()
1 parent 1a040d6 commit 6bb4feb

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

superset/mcp_service/flask_singleton.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@
3939
# Create the Flask app instance - this is the singleton
4040
app = create_app()
4141

42-
# Apply MCP-specific defaults to app.config if not already set
43-
mcp_config = get_mcp_config()
44-
for key, value in mcp_config.items():
45-
if key not in app.config:
46-
app.config[key] = value
42+
# Apply MCP configuration - reads from app.config first, falls back to defaults
43+
mcp_config = get_mcp_config(app.config)
44+
app.config.update(mcp_config)
4745

4846
logger.info("Flask app instance created successfully")
4947

superset/mcp_service/mcp_config.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,49 @@ def generate_secret_key() -> str:
7373
return secrets.token_urlsafe(42)
7474

7575

76-
def get_mcp_config() -> Dict[str, Any]:
77-
"""Get complete MCP configuration dictionary"""
78-
config = {}
79-
80-
# Add MCP-specific settings
81-
config.update(
82-
{
83-
"MCP_ADMIN_USERNAME": MCP_ADMIN_USERNAME,
84-
"MCP_DEV_USERNAME": MCP_DEV_USERNAME,
85-
"SUPERSET_WEBSERVER_ADDRESS": SUPERSET_WEBSERVER_ADDRESS,
86-
"WEBDRIVER_BASEURL": WEBDRIVER_BASEURL,
87-
"WEBDRIVER_BASEURL_USER_FRIENDLY": WEBDRIVER_BASEURL_USER_FRIENDLY,
88-
"MCP_SERVICE_HOST": MCP_SERVICE_HOST,
89-
"MCP_SERVICE_PORT": MCP_SERVICE_PORT,
90-
"MCP_DEBUG": MCP_DEBUG,
91-
}
92-
)
93-
94-
# Add session and CSRF config
95-
config.update(MCP_SESSION_CONFIG)
96-
config.update(MCP_CSRF_CONFIG)
97-
98-
return config
76+
def get_mcp_config(app_config: Dict[str, Any] | None = None) -> Dict[str, Any]:
77+
"""
78+
Get complete MCP configuration dictionary.
79+
80+
Reads from app_config first, then falls back to defaults if values are not provided.
81+
82+
Args:
83+
app_config: Optional Flask app configuration dict to read values from
84+
"""
85+
app_config = app_config or {}
86+
87+
# Default MCP configuration
88+
defaults = {
89+
"MCP_ADMIN_USERNAME": MCP_ADMIN_USERNAME,
90+
"MCP_DEV_USERNAME": MCP_DEV_USERNAME,
91+
"SUPERSET_WEBSERVER_ADDRESS": SUPERSET_WEBSERVER_ADDRESS,
92+
"WEBDRIVER_BASEURL": WEBDRIVER_BASEURL,
93+
"WEBDRIVER_BASEURL_USER_FRIENDLY": WEBDRIVER_BASEURL_USER_FRIENDLY,
94+
"MCP_SERVICE_HOST": MCP_SERVICE_HOST,
95+
"MCP_SERVICE_PORT": MCP_SERVICE_PORT,
96+
"MCP_DEBUG": MCP_DEBUG,
97+
**MCP_SESSION_CONFIG,
98+
**MCP_CSRF_CONFIG,
99+
}
100+
101+
# Merge app_config over defaults - app_config takes precedence
102+
return {**defaults, **{k: v for k, v in app_config.items() if k in defaults}}
103+
104+
105+
def get_mcp_config_with_overrides(
106+
app_config: Dict[str, Any] | None = None,
107+
) -> Dict[str, Any]:
108+
"""
109+
Alternative approach: Allow any app_config keys, not just predefined ones.
110+
111+
This version lets users add custom MCP config keys in superset_config.py
112+
that aren't predefined in the defaults.
113+
"""
114+
app_config = app_config or {}
115+
defaults = get_mcp_config()
116+
117+
# Start with defaults, then overlay any app_config values
118+
return {**defaults, **app_config}
99119

100120

101121
def get_mcp_factory_config() -> Dict[str, Any]:

0 commit comments

Comments
 (0)