mcp-test-calculator 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ # 先配置docker内置镜像加速拉取基础镜像,构建阶段pip用华为云源
2
+ FROM python:3.11-slim
3
+ WORKDIR /app
4
+ COPY requirements.txt .
5
+ # 华为云pip临时源
6
+ RUN pip install --no-cache-dir --upgrade pip \
7
+ -i https://mirrors.huaweicloud.com/repository/pypi/simple \
8
+ --trusted-host mirrors.huaweicloud.com \
9
+ && pip install --no-cache-dir -r requirements.txt \
10
+ -i https://mirrors.huaweicloud.com/repository/pypi/simple \
11
+ --trusted-host mirrors.huaweicloud.com
12
+ COPY mcp_server.py .
13
+ EXPOSE 8080
14
+ CMD ["uvicorn", "mcp_server:app", "--host", "0.0.0.0", "--port", "8080", "--proxy-headers", "--forwarded-allow-ips", "*"]
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.5
2
+ Name: mcp-test-calculator
3
+ Version: 0.1.0
4
+ Summary: MCP calculator server with add/subtract/multiply/divide tools
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.10
7
+ Requires-Dist: mcp>=2.0.0
@@ -0,0 +1,57 @@
1
+ import logging
2
+ import os
3
+ import uvicorn
4
+ from mcp.server import transport_security
5
+ from mcp.server.fastmcp import FastMCP
6
+ from mcp.server.streamable_http_manager import TransportSecuritySettings
7
+ from starlette.requests import Request
8
+ from starlette.responses import JSONResponse
9
+ # 配置日志
10
+ logging.basicConfig(level=logging.INFO)
11
+ logger = logging.getLogger(__name__)
12
+ transport_security.TransportSecurityMiddleware._validate_host = lambda _self, _host: True
13
+ logger.info("MCP Host header validation patched to allow all hosts")
14
+ # 创建 MCP 服务器实例
15
+ mcp = FastMCP(
16
+ "calculator-mcp-server",
17
+ streamable_http_path="/mcp",
18
+ stateless_http=True,
19
+ transport_security=TransportSecuritySettings(
20
+ enable_dns_rebinding_protection=False,
21
+ ),
22
+ )
23
+ logger.info(f"MCP transport_security settings: {mcp.settings.transport_security}")
24
+ @mcp.tool()
25
+ def add(a: float, b: float) -> float:
26
+ """两个数字相加"""
27
+ logger.info(f"add called: {a} + {b}")
28
+ return a + b
29
+ @mcp.tool()
30
+ def subtract(a: float, b: float) -> float:
31
+ """两个数字相减"""
32
+ logger.info(f"subtract called: {a} - {b}")
33
+ return a - b
34
+ @mcp.tool()
35
+ def multiply(a: float, b: float) -> float:
36
+ """两个数字相乘"""
37
+ logger.info(f"multiply called: {a} * {b}")
38
+ return a * b
39
+ @mcp.tool()
40
+ def divide(a: float, b: float) -> float:
41
+ """两个数字相除,除数不能为0"""
42
+ logger.info(f"divide called: {a} / {b}")
43
+ if b == 0:
44
+ raise ValueError("除数不能为0")
45
+ return a / b
46
+ @mcp.custom_route("/ping", methods=["GET"])
47
+ async def ping(_request: Request) -> JSONResponse:
48
+ """网关探活 ping 接口"""
49
+ return JSONResponse({"status": "ok", "message": "pong"})
50
+ # 导出 Streamable HTTP ASGI 应用
51
+ app = mcp.streamable_http_app()
52
+ def main() -> None:
53
+ """命令行入口,供 uvicorn / 控制台脚本调用。"""
54
+ port = int(os.getenv("PORT", 8080))
55
+ uvicorn.run(app, host="0.0.0.0", port=port)
56
+ if __name__ == "__main__":
57
+ main()
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "mcp-test-calculator"
7
+ version = "0.1.0"
8
+ description = "MCP calculator server with add/subtract/multiply/divide tools"
9
+ requires-python = ">=3.10"
10
+ license = "MIT"
11
+ dependencies = [
12
+ "mcp>=2.0.0",
13
+ ]
14
+
15
+ [project.scripts]
16
+ mcp-test-calculator = "mcp_calculator.server:main"
17
+
18
+ [tool.hatch.build.targets.wheel]
19
+ packages = ["src/mcp_calculator"]
@@ -0,0 +1,2 @@
1
+ mcp==1.28.1
2
+ uvicorn==0.46.0
@@ -0,0 +1,3 @@
1
+ from .server import mcp
2
+
3
+ __all__ = ["mcp"]
@@ -0,0 +1,43 @@
1
+ import logging
2
+ from mcp.server import MCPServer
3
+
4
+ logging.basicConfig(level=logging.INFO)
5
+ logger = logging.getLogger(__name__)
6
+
7
+ # 创建 MCP 服务器实例 (v2: MCPServer)
8
+ mcp = MCPServer("calculator-mcp-server")
9
+
10
+
11
+ @mcp.tool()
12
+ def add(a: float, b: float) -> float:
13
+ """两个数字相加"""
14
+ return a + b
15
+
16
+
17
+ @mcp.tool()
18
+ def subtract(a: float, b: float) -> float:
19
+ """两个数字相减"""
20
+ return a - b
21
+
22
+
23
+ @mcp.tool()
24
+ def multiply(a: float, b: float) -> float:
25
+ """两个数字相乘"""
26
+ return a * b
27
+
28
+
29
+ @mcp.tool()
30
+ def divide(a: float, b: float) -> float:
31
+ """两个数字相除,除数不能为0"""
32
+ if b == 0:
33
+ raise ValueError("除数不能为0")
34
+ return a / b
35
+
36
+
37
+ def main() -> None:
38
+ """UVX 入口点 — 以 stdio 模式启动 MCP 服务"""
39
+ mcp.run(transport="stdio")
40
+
41
+
42
+ if __name__ == "__main__":
43
+ main()