mirror of
https://github.com/Sun-ZhenXing/mcp-template-python.git
synced 2026-02-04 10:13:31 +00:00
init repo
This commit is contained in:
1
src/mcp_template_python/__about__.py
Normal file
1
src/mcp_template_python/__about__.py
Normal file
@@ -0,0 +1 @@
|
||||
__version__ = "0.1.0"
|
||||
5
src/mcp_template_python/__init__.py
Normal file
5
src/mcp_template_python/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from .__about__ import __version__
|
||||
|
||||
__all__ = [
|
||||
"__version__",
|
||||
]
|
||||
44
src/mcp_template_python/__main__.py
Normal file
44
src/mcp_template_python/__main__.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Run a MarkItDown MCP server")
|
||||
|
||||
parser.add_argument(
|
||||
"--http",
|
||||
action="store_true",
|
||||
help="Run the server with Streamable HTTP and SSE transport rather than STDIO (default: False)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--host", default=None, help="Host to bind to (default: 127.0.0.1)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--port", type=int, default=None, help="Port to listen on (default: 3001)"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.http and (args.host or args.port):
|
||||
parser.error(
|
||||
"Host and port arguments are only valid when using streamable HTTP or SSE transport (see: --http)."
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
if args.http:
|
||||
import uvicorn
|
||||
|
||||
from .server import app
|
||||
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=args.host or "127.0.0.1",
|
||||
port=args.port or 3001,
|
||||
)
|
||||
else:
|
||||
from .app.math import mcp
|
||||
|
||||
mcp.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
0
src/mcp_template_python/app/__init__.py
Normal file
0
src/mcp_template_python/app/__init__.py
Normal file
41
src/mcp_template_python/app/math.py
Normal file
41
src/mcp_template_python/app/math.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from operator import add, mul, sub, truediv
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
mcp = FastMCP("math")
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def add_nums(a: float, b: float) -> float:
|
||||
"""
|
||||
Adds two numbers.
|
||||
"""
|
||||
return add(a, b)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def sub_nums(a: float, b: float) -> float:
|
||||
"""
|
||||
Subtracts the second number from the first.
|
||||
"""
|
||||
return sub(a, b)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def mul_nums(a: float, b: float) -> float:
|
||||
"""
|
||||
Multiplies two numbers.
|
||||
"""
|
||||
return mul(a, b)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def div_nums(a: float, b: float) -> float:
|
||||
"""
|
||||
Divides the first number by the second.
|
||||
"""
|
||||
return truediv(a, b)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mcp.run()
|
||||
0
src/mcp_template_python/py.typed
Normal file
0
src/mcp_template_python/py.typed
Normal file
24
src/mcp_template_python/server.py
Normal file
24
src/mcp_template_python/server.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import contextlib
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from .app.math import mcp as math
|
||||
|
||||
MCP_MAP = {
|
||||
"math": math,
|
||||
}
|
||||
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
async with contextlib.AsyncExitStack() as stack:
|
||||
for mcp in MCP_MAP.values():
|
||||
await stack.enter_async_context(mcp.session_manager.run())
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
for name, mcp in MCP_MAP.items():
|
||||
app.mount(f"/{name}/compatible", mcp.sse_app())
|
||||
app.mount(f"/{name}", mcp.streamable_http_app())
|
||||
Reference in New Issue
Block a user