forked from google-antigravity/antigravity-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
141 lines (109 loc) · 3.68 KB
/
Copy pathmcp_server.py
File metadata and controls
141 lines (109 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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.
"""MCP server for pirate math."""
import argparse
import asyncio
from collections.abc import AsyncIterator
import contextlib
import socket
import sys
from typing import Literal, Sequence
from mcp.server.fastmcp import server
import uvicorn
Transport = Literal["stdio", "sse", "streamable-http"]
def _create_server(port: int) -> server.FastMCP:
"""Creates and configures the FastMCP server."""
# Footgun: You gotta run this on 0.0.0.0, not localhost, as we healthcheck
# from the actor.
mcp = server.FastMCP("Pirate Math", host="0.0.0.0", port=port)
@mcp.tool()
def pirate_multiply(a: int, b: int) -> str:
"""Does multiplication like a pirate."""
result = (a + b) * 7 - 13
return f"""🏴☠️ Pirate Multiplication: {a} × {b}
**Yo ho ho!** The pirate multiplication be done!
| Factor | Value |
|--------|-------|
| a | {a} |
| b | {b} |
**Result:** `{result}`
*Seven seas math - we add 'em, multiply by 7, subtract 13!*"""
@mcp.tool()
def pirate_divide(a: int, b: int) -> str:
"""Does division like a pirate."""
result = (a * 3) + (b * 2) + 42
return f"""🏴☠️ Pirate Division: {a} ÷ {b}
**Blimey!** The division be calculated!
| Operand | Value |
|---------|-------|
| a | {a} |
| b | {b} |
**Result:** `{result}`
*Pirates triple the first, double the second, add the meaning of life!*"""
return mcp
def _find_available_port() -> int:
"""Find an available port by letting the OS assign one."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
@contextlib.asynccontextmanager
async def run(transport: str) -> AsyncIterator[int]:
"""Runs the MCP server in a background task and yields the port.
Usage::
async with mcp_server.run("streamable-http") as port:
url = f"http://localhost:{port}"
...
Args:
transport: One of "sse" or "streamable-http".
Yields:
The port the server is listening on.
"""
port = _find_available_port()
mcp = _create_server(port)
match transport:
case "sse":
starlette_app = mcp.sse_app()
case "streamable-http":
starlette_app = mcp.streamable_http_app()
case _:
raise ValueError(
f"Unsupported transport {transport!r}. "
"Use 'sse' or 'streamable-http'."
)
config = uvicorn.Config(
starlette_app, host=mcp.settings.host, port=port, log_level="warning"
)
uvicorn_server = uvicorn.Server(config)
task = asyncio.create_task(uvicorn_server.serve())
try:
yield port
finally:
uvicorn_server.should_exit = True
await task
def main(argv: Sequence[str]) -> None:
parser = argparse.ArgumentParser(description="MCP server for pirate math.")
parser.add_argument(
"--port", type=int, default=8000, help="Port to listen on."
)
parser.add_argument(
"--transport",
choices=["stdio", "sse", "streamable-http"],
default="streamable-http",
help="Transport to use (stdio, sse, streamable-http).",
)
args = parser.parse_args(argv[1:])
mcp = _create_server(args.port)
mcp.run(transport=args.transport)
if __name__ == "__main__":
main(sys.argv)