-
Notifications
You must be signed in to change notification settings - Fork 7
mcp-hmr
#39
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
mcp-hmr
#39
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c267517
feat: add initial implementation of `mcp-hmr` ✨
CNSeniorious000 f5fdbd1
Update packages/mcp-hmr/mcp_hmr.py
CNSeniorious000 45008ee
Update packages/mcp-hmr/pyproject.toml
CNSeniorious000 68ca61e
Update packages/mcp-hmr/mcp_hmr.py
CNSeniorious000 3a1b25b
chore: add `mcp-hmr` to workspace dependencies
CNSeniorious000 adeb32a
feat: add a `README.md` for `mcp-hmr`
CNSeniorious000 74e0ca3
fix(ci): publish `mcp-hmr` too
CNSeniorious000 baeb813
chore: give the `base_app` a name
CNSeniorious000 fad2b48
feat: add `--log-level` option
CNSeniorious000 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # mcp-hmr | ||
|
|
||
| [](https://pypi.org/project/mcp-hmr/) | ||
| [](https://pepy.tech/projects/mcp-hmr) | ||
|
|
||
| Provides [Hot Module Reloading](https://pyth-on-line.promplate.dev/hmr) for MCP/FastMCP servers. | ||
|
|
||
| It acts as **a drop-in replacement for `mcp run module:app` or `fastmcp run module:app`.** Both [FastMCP v2](https://github.com/jlowin/fastmcp) and the [official python SDK](https://github.com/modelcontextprotocol/python-sdk) are supported. | ||
|
|
||
| ## Usage | ||
|
|
||
| If your server instance is named `app` in `path/to/main.py`, you can run: | ||
|
|
||
| ```sh | ||
| mcp-hmr path.to.main:app | ||
| ``` | ||
|
|
||
| Which will be equivalent to the following code but with [HMR](https://github.com/promplate/hmr) enabled: | ||
|
|
||
| ```py | ||
| from path.to.main import app | ||
|
|
||
| app.run("stdio") | ||
| ``` | ||
|
|
||
| Now, whenever you save changes to your source code, the server will automatically reload without dropping the connection to the client. |
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,116 @@ | ||
| import sys | ||
|
|
||
| __version__ = "0.0.1" | ||
|
|
||
|
|
||
| async def run_with_hmr(target: str, log_level: str | None = None): | ||
| module, attr = target.split(":") | ||
|
|
||
| from asyncio import Event, Lock, create_task | ||
| from contextlib import contextmanager | ||
| from importlib import import_module | ||
|
|
||
| import mcp.server | ||
| from fastmcp import FastMCP | ||
| from fastmcp.server.proxy import ProxyClient | ||
| from reactivity import async_effect, derived | ||
| from reactivity.hmr.core import HMR_CONTEXT, AsyncReloader | ||
| from reactivity.hmr.hooks import call_post_reload_hooks, call_pre_reload_hooks | ||
|
|
||
| base_app = FastMCP(name="proxy", include_fastmcp_meta=False) | ||
|
|
||
| @contextmanager | ||
| def mount(app: FastMCP | mcp.server.FastMCP): | ||
| base_app.mount(proxy := FastMCP.as_proxy(ProxyClient(app)), as_proxy=False) | ||
| try: | ||
| yield | ||
| finally: # unmount | ||
|
CNSeniorious000 marked this conversation as resolved.
|
||
| for mounted_server in list(base_app._mounted_servers): # noqa: SLF001 | ||
| if mounted_server.server is proxy: | ||
| base_app._mounted_servers.remove(mounted_server) # noqa: SLF001 | ||
| base_app._tool_manager._mounted_servers.remove(mounted_server) # noqa: SLF001 | ||
| base_app._resource_manager._mounted_servers.remove(mounted_server) # noqa: SLF001 | ||
| base_app._prompt_manager._mounted_servers.remove(mounted_server) # noqa: SLF001 | ||
| break | ||
|
|
||
| lock = Lock() | ||
|
|
||
| async def using(app: FastMCP | mcp.server.FastMCP, stop_event: Event, finish_event: Event): | ||
| async with lock: | ||
| with mount(app): | ||
| await stop_event.wait() | ||
| finish_event.set() | ||
|
|
||
| @derived(context=HMR_CONTEXT) | ||
| def get_app(): | ||
| return getattr(import_module(module), attr) | ||
|
|
||
| stop_event: Event | None = None | ||
| finish_event: Event = ... # type: ignore | ||
|
CNSeniorious000 marked this conversation as resolved.
CNSeniorious000 marked this conversation as resolved.
|
||
|
|
||
|
CNSeniorious000 marked this conversation as resolved.
|
||
| @async_effect(context=HMR_CONTEXT, call_immediately=False) | ||
| async def main(): | ||
| nonlocal stop_event, finish_event | ||
|
|
||
| if stop_event is not None: | ||
| stop_event.set() | ||
| await finish_event.wait() | ||
|
|
||
| app = get_app() | ||
|
|
||
| create_task(using(app, stop_event := Event(), finish_event := Event())) # noqa: RUF006 | ||
|
CNSeniorious000 marked this conversation as resolved.
|
||
|
|
||
| class Reloader(AsyncReloader): | ||
| def __init__(self): | ||
| super().__init__("") | ||
| self.error_filter.exclude_filenames.add(__file__) | ||
|
|
||
| async def __aenter__(self): | ||
| call_pre_reload_hooks() | ||
| try: | ||
| await main() | ||
| finally: | ||
| call_post_reload_hooks() | ||
| self.reloader_task = create_task(self.start_watching()) | ||
|
|
||
| async def __aexit__(self, *_): | ||
| self.stop_watching() | ||
| main.dispose() | ||
| await self.reloader_task | ||
|
|
||
| async with Reloader(): | ||
| await base_app.run_stdio_async(show_banner=False, log_level=log_level) | ||
|
|
||
|
|
||
| def cli(argv: list[str] = sys.argv[1:]): | ||
| from argparse import SUPPRESS, ArgumentParser | ||
|
|
||
| parser = ArgumentParser(prog="mcp-hmr", description="Hot Reloading for MCP Servers • Automatically reload on code changes") | ||
| parser.add_argument("target", help="The import path of the FastMCP instance, e.g. `main:app` means `from main import app`", metavar="module:attr") | ||
| parser.add_argument("-l", "--log-level", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], type=str.upper, default=None) | ||
| parser.add_argument("--version", action="version", version=f"mcp-hmr {__version__}", help=SUPPRESS) | ||
|
|
||
| if not argv: | ||
| parser.print_help() | ||
| return | ||
|
|
||
| args = parser.parse_args(argv) | ||
|
|
||
| target: str = args.target | ||
|
|
||
| if target.count(":") != 1 or target.startswith(":") or target.endswith(":"): | ||
| parser.exit(1, f"The target argument must be in the format 'module:attr', e.g. 'main:app'. Got: '{target}'") | ||
|
|
||
| from asyncio import run | ||
| from contextlib import suppress | ||
| from pathlib import Path | ||
|
|
||
| if (cwd := str(Path.cwd())) not in sys.path: | ||
| sys.path.append(cwd) | ||
|
|
||
| with suppress(KeyboardInterrupt): | ||
| run(run_with_hmr(target, args.log_level)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli() | ||
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,33 @@ | ||
| [project] | ||
| name = "mcp-hmr" | ||
| description = "Hot Reloading for MCP Servers" | ||
| readme = "README.md" | ||
| requires-python = ">=3.12" | ||
| keywords = ["mcp", "hot-reload", "hmr", "reload", "server"] | ||
| authors = [{ name = "Muspi Merol", email = "me@promplate.dev" }] | ||
| license = { text = "MIT" } | ||
| classifiers = [ | ||
| "Development Status :: 2 - Pre-Alpha", | ||
| "Intended Audience :: Developers", | ||
| "Operating System :: OS Independent", | ||
| ] | ||
| dependencies = [ | ||
| "fastmcp>=2.2.0,<3", | ||
| "hmr~=0.7.0", | ||
| ] | ||
|
CNSeniorious000 marked this conversation as resolved.
|
||
| dynamic = ["version"] | ||
|
|
||
| [tool.pdm.version] | ||
| source = "file" | ||
| path = "mcp_hmr.py" | ||
|
|
||
| [project.scripts] | ||
| mcp-hmr = "mcp_hmr:cli" | ||
|
|
||
| [project.urls] | ||
| Homepage = "https://github.com/promplate/hmr" | ||
|
|
||
| [build-system] | ||
| requires = ["pdm-backend"] | ||
| build-backend = "pdm.backend" | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.