Skip to main content
Glama

SampleMCP

一个简单的 Python 项目,演示了如何使用 FastMCP、LangChain MCP 适配器、LangGraph 和 OpenAI 构建并连接 MCP 服务器。

功能

  • 使用 stdio 的数学 MCP 服务器

  • 使用 streamable-http 的天气 MCP 服务器

  • 连接多个 MCP 服务器的 LangChain MCP 客户端

  • 使用 MCP 工具的 LangGraph ReAct 代理

  • 数学和天气查询的工具调用示例

Related MCP server: Server MCP

项目结构

SampleMCP/
├── client.py          # MCP client + LangGraph agent
├── mathserver.py      # Math MCP server with add/multiply tools
├── weather.py         # Weather MCP server using streamable HTTP
├── main.py            # Basic sample entry point
├── pyproject.toml     # Project metadata and dependencies
├── requirements.txt   # Python dependencies
└── README.md

要求

  • Python 3.12+

  • OpenAI API 密钥

  • 可选:Groq API 密钥

安装

克隆仓库:

git clone https://github.com/JayantPrakash/SampleMCP.git
cd SampleMCP

安装依赖:

pip install -r requirements.txt

或者使用 uv:

uv sync

环境变量

在项目根目录下创建一个 .env 文件:

OPENAI_API_KEY=your_openai_api_key
GROQ_API_KEY=your_groq_api_key

运行 MCP 服务器

1. 启动天气服务器

天气服务器使用 streamable-http。

python weather.py

默认情况下,它在以下地址暴露 MCP 端点:

http://localhost:8000/mcp

2. 数学服务器

数学服务器使用 stdio,并由客户端通过以下方式自动启动:

"command": "python",
"args": ["mathserver.py"],
"transport": "stdio"

运行客户端

在另一个终端中,运行:

python client.py

客户端连接到:

  • 通过 stdio 连接的 mathserver.py

  • 通过 streamable_http 连接的 weather.py

然后它会创建一个 LangGraph ReAct 代理并询问:

what's (3 + 5) x 12?

以及:

what is the weather in California?

MCP 工具

数学服务器

定义在 mathserver.py 中。

add(a: int, b: int) -> int

将两个数字相加。

multiple(a: int, b: int) -> int

将两个数字相乘。

天气服务器

定义在 weather.py 中。

get_weather(location: str) -> str

返回给定地点的示例天气响应。

注意事项

  • mathserver.py 使用 transport="stdio"。

  • weather.py 使用 transport="streamable-http"。

  • 在 client.py 中,可流式 HTTP 传输配置为 streamable_http。

  • 在执行 client.py 之前,请确保天气服务器正在运行。

示例输出

Math response: 96
Weather response: It's always raining in California

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    Demonstrates an MCP server with calculate and text_stats tools over Streamable HTTP, integrating with LangChain/LangGraph and n8n agents.
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    Provides math and weather tools accessible via LangGraph agent using MCP protocol with stdio and streamable HTTP transports.
    1
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    A model-agnostic MCP server exposing example tools (add1, multiply2, greet) for learning purposes, working with any LLM through stdio transport.
    -