-
Notifications
You must be signed in to change notification settings - Fork 1k
chore: Split MCP Server into 2 servers; One for recipe (A2UI over MCP… #879
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
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Calculator MCP App Demo | ||
|
|
||
| A demo of an MCP server exposing a Calculator as an MCP Application Resource. | ||
|
|
||
| ## Usage | ||
|
|
||
| 1. Start the server using either stdio (default) or SSE transport: | ||
|
|
||
| ```bash | ||
| # Using SSE transport (default) on port 8000 | ||
| uv run . | ||
| ``` | ||
|
|
||
| The server exposes a resource named `ui://calculator/app`. | ||
|
|
||
| 2. Inspect the server using the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) | ||
|
|
||
| ```bash | ||
| npx @modelcontextprotocol/inspector | ||
| ``` | ||
|
|
||
| Connect to http://localhost:8000/sse using Transport Type SSE and fetch the resources. |
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
File renamed without changes.
File renamed without changes.
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,53 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| [project] | ||
| name = "a2ui-mcp-calculator-demo" | ||
| version = "0.1.0" | ||
| description = "A demo of exposing a calculator app via MCP" | ||
| readme = "README.md" | ||
| requires-python = ">=3.10" | ||
| keywords = ["mcp", "automation", "agent"] | ||
| license = { text = "MIT" } | ||
| dependencies = ["anyio>=4.5", "click>=8.2.0", "httpx>=0.27", "mcp"] | ||
|
|
||
| [project.scripts] | ||
| a2ui-mcp-calculator-demo = "server:main" | ||
|
|
||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["."] | ||
|
|
||
| [[tool.uv.index]] | ||
| url = "https://pypi.org/simple" | ||
| default = true | ||
|
|
||
| [tool.pyright] | ||
| include = ["."] | ||
| venvPath = "." | ||
| venv = ".venv" | ||
|
|
||
| [tool.ruff.lint] | ||
| select = ["E", "F", "I"] | ||
| ignore = [] | ||
|
|
||
| [tool.ruff] | ||
| line-length = 120 | ||
| target-version = "py310" | ||
|
|
||
| [dependency-groups] | ||
| dev = ["pyright>=1.1.378", "pytest>=8.3.3", "ruff>=0.6.9"] |
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,101 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import Any | ||
| import anyio | ||
| import click | ||
| import pathlib | ||
| import mcp.types as types | ||
| from mcp.server.lowlevel import Server | ||
| from starlette.requests import Request | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option("--port", default=8000, help="Port to listen on for SSE") | ||
| @click.option( | ||
| "--transport", | ||
| type=click.Choice(["stdio", "sse"]), | ||
| default="sse", | ||
| help="Transport type", | ||
| ) | ||
| def main(port: int, transport: str) -> int: | ||
|
|
||
| app = Server("a2ui-mcp-calculator-demo") | ||
|
|
||
| @app.list_resources() | ||
| async def list_resources() -> list[types.Resource]: | ||
| return [ | ||
| types.Resource( | ||
| uri="ui://calculator/app", | ||
| name="Calculator App", | ||
| mimeType="text/html;profile=mcp-app", | ||
| description="A simple calculator application", | ||
| ) | ||
| ] | ||
|
|
||
| @app.read_resource() | ||
| async def read_resource(uri: types.ResourceIdentifier) -> str | bytes: | ||
| if str(uri) == "ui://calculator/app": | ||
| try: | ||
| return (pathlib.Path(__file__).parent / "apps" / "calculator.html").read_text() | ||
| except FileNotFoundError: | ||
| raise ValueError(f"Resource file not found for uri: {uri}") | ||
| raise ValueError(f"Unknown resource: {uri}") | ||
|
|
||
| if transport == "sse": | ||
| from mcp.server.sse import SseServerTransport | ||
| from starlette.applications import Starlette | ||
| from starlette.responses import Response | ||
| from starlette.routing import Mount, Route | ||
| from starlette.middleware import Middleware | ||
| from starlette.middleware.cors import CORSMiddleware | ||
|
|
||
| sse = SseServerTransport("/messages/") | ||
|
|
||
| async def handle_sse(request: Request): | ||
| async with sse.connect_sse(request.scope, request.receive, request._send) as streams: # type: ignore[reportPrivateUsage] | ||
| await app.run(streams[0], streams[1], app.create_initialization_options()) | ||
| return Response() | ||
|
|
||
| starlette_app = Starlette( | ||
| debug=True, | ||
| routes=[ | ||
| Route("/sse", endpoint=handle_sse, methods=["GET"]), | ||
| Mount("/messages/", app=sse.handle_post_message), | ||
| ], | ||
| middleware=[ | ||
| Middleware( | ||
| CORSMiddleware, | ||
| allow_origins=["*"], | ||
| allow_methods=["*"], | ||
| allow_headers=["*"], | ||
sugoi-yuzuru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ], | ||
| ) | ||
|
|
||
| import uvicorn | ||
|
|
||
| print(f"Server running at 127.0.0.1:{port} using sse") | ||
sugoi-yuzuru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| uvicorn.run(starlette_app, host="127.0.0.1", port=port) | ||
| else: | ||
| from mcp.server.stdio import stdio_server | ||
|
|
||
| async def arun(): | ||
| async with stdio_server() as streams: | ||
| await app.run(streams[0], streams[1], app.create_initialization_options()) | ||
|
|
||
| click.echo("Server running using stdio", err=True) | ||
| anyio.run(arun) | ||
|
|
||
| return 0 | ||
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.
Uh oh!
There was an error while loading. Please reload this page.