使用说明
mcp服务器示例
FastAPI MCP(模型上下文协议)服务器 FastMCP。通过流式http传输公开数学和性能测试工具。
MCP工具
数学(/math_mcp)
| 工具 | 说明 |
|---|---|
add |
加两个数字 |
subtract |
从a中减去b |
multiply |
将两个数字相乘 |
divide |
将a除以b |
power |
将基数提高到指数幂 |
sqrt |
计算一个数字的平方根 |
modulo |
计算模b |
factorial |
计算非负整数的阶乘 |
性能(/perf-mcp)
| 工具 | 说明 |
|---|---|
run_perf_test |
对具有并发请求的URL运行负载测试 |
项目结构
server.py — FastAPI app entry point with REST routes and MCP mount
server/ — MCP server definitions
math_tools.py — Math MCP server with tool definitions
perf_tools.py — Perf MCP server with load testing tool
client/ — LangChain agent clients
math_client.py — Interactive LangChain agent client (math)
perf_client.py — Interactive LangChain agent client (perf)
llm_factory.py — LLM provider factory (ollama, openai, gemini)
requirements.txt — Python dependencies
Dockerfile — Container image definition
Makefile — Build and run shortcuts
deployment/ — Helm chart for Kubernetes deployment
入门指南
需求
- Python 3.13+
在本地运行
pip install -r requirements.txt
python server.py
服务器启动于 http://localhost:58080 与:
- 根端点位于
/列出可用端点 - 数学MCP端点位于
/math_mcp(可流式传输http) - Perf MCP端点位于
/perf-mcp(可流式传输http) - 位于的REST API终结点
/add,/subtract,/multiply,/divide,/power,/sqrt,/modulo,/factorial,/run_perf_test - API文档,网址:
/docs
运行客户端
客户端使用LangChain代理通过交互式REPL与MCP服务器交互。通过配置LLM提供程序 LLM_PROVIDER env-var和服务器URL通过 MCP_SERVER_URL.
| 环境变量 | 描述 | 默认值 |
|---|---|---|
LLM_PROVIDER |
要使用的LLM提供者 | ollama |
MCP_SERVER_URL |
MCP服务器基本URL | http://localhost:58080 |
支持的LLM提供商:
| 提供者 | 型号 |
|---|---|
ollama |
llama3.1 |
openai |
gpt-4.1 |
gemini |
gemini-2.5-flash |
python -m client.math_client
python -m client.perf_client
LLM_PROVIDER=openai python -m client.math_client
使用Docker运行
docker build -t mcp-server-example .
docker run -p 58080:58080 mcp-server-example
使用Helm部署
cd deployment/k8s/helm
helm install mcp-server-example ./mcp-server-example
使用Helm升级
cd deployment/k8s/helm
make upgrade
生成文件目标
| 目标 | 描述 |
|---|---|
install-deps |
安装Python依赖项 |
image |
构建Docker镜像 |
run |
运行Docker容器 |
run-shell |
在新容器中启动shell |
exec-shell |
执行到正在运行的容器中 |
check |
使用pytest运行测试 |
clean |
删除生成的文件 |
up |
构建并运行(check + image + run) |
CI/CD
GitHub行动 工作流 跑步推 main 以及拉取请求。它使用Python 3.13构建项目,并在非PR事件时将Docker镜像推送到Docker Hub。
