Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/agent/adk/mcp_app_proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ create a simple "MCP Apps Proxy" agent that is hosted as an A2A server.
## Running the sample

1. Run the MCP Server that serves the MCP Apps. ([Link to
instructions](../../agent/mcp/README.md))
instructions](../../mcp/calculator/README.md))


2. Navigate to the samples directory:
Expand Down
2 changes: 1 addition & 1 deletion samples/agent/adk/orchestrator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Subagents are configured using RemoteA2aAgent which translates ADK events to A2A
Optionally, run the MCP Server and MCP App Proxy Agent to MCP Apps in A2UI demo:

```bash
cd samples/agent/mcp
cd samples/agent/mcp/calculator
uv run . --port=8000
```

Expand Down
22 changes: 22 additions & 0 deletions samples/agent/mcp/calculator/README.md
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.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2025 Google LLC
# 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.
Expand All @@ -11,3 +11,5 @@
# 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.

# Empty init
File renamed without changes.
53 changes: 53 additions & 0 deletions samples/agent/mcp/calculator/pyproject.toml
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"]
101 changes: 101 additions & 0 deletions samples/agent/mcp/calculator/server.py
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=["*"],
)
],
)

import uvicorn

print(f"Server running at 127.0.0.1:{port} using sse")
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
Loading
Loading