-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Implement configuration function to customize MCP server #3796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| """Dash MCP (Model Context Protocol) server integration.""" | ||
|
|
||
| from dash.mcp._configure import configure_mcp_server | ||
| from dash.mcp._decorator import mcp_enabled | ||
| from dash.mcp._server import enable_mcp_server | ||
|
|
||
| __all__ = [ | ||
| "configure_mcp_server", | ||
| "enable_mcp_server", | ||
| "mcp_enabled", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| """Public configuration API for the Dash MCP server.""" | ||
|
|
||
| # pylint: disable=cyclic-import | ||
| # dash.dash lazy-imports dash.mcp inside _setup_routes(); pylint's static | ||
| # analysis treats it as a module-level import, producing a false cycle. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from dash import get_app | ||
| from dash.exceptions import AppNotFoundError | ||
| from dash.mcp.primitives.resources import _RESOURCE_PROVIDERS as MCP_RESOURCE_PROVIDERS | ||
| from dash.mcp.primitives.resources.resource_clientside_callbacks import ( | ||
| ClientsideCallbacksResource, | ||
| ) | ||
| from dash.mcp.primitives.resources.resource_components import ComponentsResource | ||
| from dash.mcp.primitives.resources.resource_layout import LayoutResource | ||
| from dash.mcp.primitives.resources.resource_page_layout import PageLayoutResource | ||
| from dash.mcp.primitives.resources.resource_pages import PagesResource | ||
| from dash.mcp.primitives.tools import _TOOL_PROVIDERS as MCP_TOOL_PROVIDERS | ||
| from dash.mcp.primitives.tools.tool_get_dash_component import GetDashComponentTool | ||
| from dash.mcp.primitives.tools.tools_callbacks import CallbackTools | ||
|
|
||
| _ALL_MCP_RESOURCE_PROVIDERS = list(MCP_RESOURCE_PROVIDERS) | ||
| _ALL_MCP_TOOL_PROVIDERS = list(MCP_TOOL_PROVIDERS) | ||
|
|
||
| # Membership groupings (order-independent): which providers each toggle | ||
| # controls. The exposed order is owned solely by the registry lists. | ||
| _LAYOUT_RESOURCES = {LayoutResource, ComponentsResource} | ||
| _CLIENTSIDE_CALLBACK_RESOURCES = {ClientsideCallbacksResource} | ||
| _PAGE_RESOURCES = {PagesResource, PageLayoutResource} | ||
| _LAYOUT_TOOLS = {GetDashComponentTool} | ||
|
|
||
| _DEFAULT_CONFIG = { | ||
| "include_layout": True, | ||
| "include_callbacks": True, | ||
| "include_clientside_callbacks": True, | ||
| "include_pages": True, | ||
| "expose_callback_docstrings": False, | ||
| } | ||
| _current_config = dict(_DEFAULT_CONFIG) | ||
|
|
||
|
|
||
| def configure_mcp_server( | ||
| *, | ||
| include_layout: Optional[bool] = None, | ||
| include_callbacks: Optional[bool] = None, | ||
| include_clientside_callbacks: Optional[bool] = None, | ||
| include_pages: Optional[bool] = None, | ||
| expose_callback_docstrings: Optional[bool] = None, | ||
| ) -> None: | ||
| """ | ||
| Configure which content the Dash MCP server exposes. | ||
|
|
||
| Only the parameters that are explicitly passed are updated; any parameter | ||
| that is omitted keeps its current value. On the first call, unset values | ||
| take their defaults (all content included except callback docstrings). | ||
|
|
||
| :param include_layout: Expose ``dash://layout``, ``dash://components``, | ||
| and the ``get_dash_component`` tool. Defaults to ``True``. | ||
| :param include_callbacks: When ``True`` (default), all callbacks are | ||
| included; ``mcp_enabled=False`` on a ``@callback`` opts it out. | ||
| When ``False``, no callbacks are included by default; | ||
| ``mcp_enabled=True`` opts a specific callback in. | ||
| :param include_clientside_callbacks: Expose the | ||
| ``dash://clientside-callbacks`` resource. Defaults to ``True``. | ||
| :param include_pages: Expose ``dash://pages`` and | ||
| ``dash://page-layout/{path}``. Defaults to ``True``. | ||
| :param expose_callback_docstrings: Include callback docstrings in | ||
| tool descriptions. Defaults to ``False``. | ||
|
|
||
| Example — expose only ``@mcp_enabled``-decorated functions:: | ||
|
|
||
| from dash.mcp import configure_mcp_server | ||
|
|
||
| configure_mcp_server( | ||
| include_layout=False, | ||
| include_callbacks=False, | ||
| include_clientside_callbacks=False, | ||
| include_pages=False, | ||
| ) | ||
| """ | ||
| try: | ||
| if get_app().backend.has_request_context(): | ||
| raise RuntimeError("MCP server can't be configured within a callback") | ||
| except AppNotFoundError: | ||
| pass | ||
|
|
||
| passed = { | ||
| "include_layout": include_layout, | ||
| "include_callbacks": include_callbacks, | ||
| "include_clientside_callbacks": include_clientside_callbacks, | ||
| "include_pages": include_pages, | ||
| "expose_callback_docstrings": expose_callback_docstrings, | ||
| } | ||
| _current_config.update( | ||
| {key: value for key, value in passed.items() if value is not None} | ||
| ) | ||
|
|
||
| CallbackTools.callbacks_mcp_enabled_by_default = _current_config[ | ||
| "include_callbacks" | ||
| ] | ||
| CallbackTools.expose_docstrings_by_default = _current_config[ | ||
| "expose_callback_docstrings" | ||
| ] | ||
|
|
||
| excluded_resources: set = set() | ||
| if not _current_config["include_layout"]: | ||
| excluded_resources |= _LAYOUT_RESOURCES | ||
| if not _current_config["include_clientside_callbacks"]: | ||
| excluded_resources |= _CLIENTSIDE_CALLBACK_RESOURCES | ||
| if not _current_config["include_pages"]: | ||
| excluded_resources |= _PAGE_RESOURCES | ||
| MCP_RESOURCE_PROVIDERS[:] = [ | ||
| resource | ||
| for resource in _ALL_MCP_RESOURCE_PROVIDERS | ||
| if resource not in excluded_resources | ||
| ] | ||
|
|
||
| excluded_tools: set = set() | ||
| if not _current_config["include_layout"]: | ||
| excluded_tools |= _LAYOUT_TOOLS | ||
| MCP_TOOL_PROVIDERS[:] = [ | ||
| tool for tool in _ALL_MCP_TOOL_PROVIDERS if tool not in excluded_tools | ||
| ] | ||
|
|
||
| # Invalidate the cached callback map so it is rebuilt with the new config. | ||
| # No app yet (configured before `Dash()`) means there is no cache to clear. | ||
| try: | ||
| get_app().mcp_callback_map = None | ||
| except AppNotFoundError: | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
KoolADE85 marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually needs to be
Anyfor now because of Python < 3.10.CallbackAdapterCollectionimports from themcpmodule which cannot be installed on 3.8. Therefore, if we import it here for typing, it will break on older python versions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stupid backward compatibility!