使用说明
项目简介
本项目提供了一系列Model Context Protocol (MCP) 服务器的示例,旨在演示如何通过MCP协议将外部功能集成到大型语言模型(LLM)中。这些示例包括:
- 数学服务器 (math_server.py): 提供加法和乘法运算工具。
- 笑话服务器 (joker_server.py): 从外部API获取随机笑话。
- 天气服务器 (weather_server.py): 提供(模拟)天气信息查询功能。
这些服务器可以与兼容MCP协议的客户端配合使用,使得LLM能够通过标准化的方式调用这些工具,从而扩展其能力。
主要功能点
- 工具注册: MCP服务器允许注册各种工具(函数),例如数学计算、API调用等,供LLM客户端按需调用。
- 多样化示例: 提供了多种类型的工具示例,覆盖了计算、信息检索等常见应用场景。
- 多传输协议: 示例代码展示了如何通过不同的传输协议(如stdio和SSE)运行MCP服务器。
- 客户端示例: 仓库中包含Python客户端代码,演示了如何连接和使用这些MCP服务器,并集成了Langchain和LangGraph等流行的LLM应用框架。
- VaaS (Function-as-a-Service) 示例: 展示了如何将MCP服务器封装为serverless函数,并通过FastAPI提供Web API接口。
安装步骤
- 克隆仓库:
git clone https://github.com/zzxwill/mcp-samples cd mcp-samples - 安装依赖:
建议使用pip安装项目依赖。虽然仓库中没有明确的'requirements.txt'文件,但根据代码依赖,您可能需要安装以下Python包:
pip install mcp fastapi requests uvicorn langchain langchain_openai langchain_mcp_adapters langgraph
服务器配置
MCP客户端需要配置MCP服务器的连接信息才能正常工作。以下是不同示例服务器的配置信息,以JSON格式提供。请注意,您需要根据实际文件路径修改 'args' 中的路径。
针对 'multiple_mcp_servers' 示例的配置:
{ "math": { "command": "python", "args": ["/path/to/mcp-samples/multiple_mcp_servers/math_server.py"], // 将 "/path/to/mcp-samples" 替换为您的本地仓库绝对路径 "transport": "stdio" }, "weather": { "url": "http://localhost:8000/sse", // 天气服务器配置为通过SSE协议在端口 8000 上运行 "transport": "sse" }, "joker": { "command": "python", "args": ["/path/to/mcp-samples/multiple_mcp_servers/joker_server.py"], // 将 "/path/to/mcp-samples" 替换为您的本地仓库绝对路径 "transport": "stdio" } }
针对 'single_mcp_server' 示例的配置:
{ "math": { "command": "python", "args": ["/path/to/mcp-samples/single_mcp_server/math_server.py"], // 将 "/path/to/mcp-samples" 替换为您的本地仓库绝对路径 "transport": "stdio" } }
针对 'vefaas-function' 示例:
'vefaas-function' 示例本身是一个FastAPI应用,它充当MCP客户端。 后端的 'math_server.py' 和 'joker_server.py' 需要作为独立的MCP服务器运行。 客户端配置将体现在 'vefaas-function/client.py' 文件中,您需要修改该文件中的服务器配置。
基本使用方法
-
启动MCP服务器:
- 对于 'multiple_mcp_servers' 和 'single_mcp_server' 示例,分别在不同的终端中运行 'math_server.py'、'joker_server.py' 和 'weather_server.py' 文件。 例如,使用命令 'python multiple_mcp_servers/math_server.py' 启动数学服务器。 对于 'multiple_mcp_servers' 中的 'weather_server.py',请确保它作为SSE服务器在端口 8000 上运行。
- 对于 'vefaas-function' 示例,首先启动 'math_server.py' 和 'joker_server.py',然后运行 FastAPI 应用 'vefaas-function/client.py',使用命令 'python vefaas-function/client.py'。
-
运行MCP客户端:
- 对于 'multiple_mcp_servers' 示例,运行 'python multiple_mcp_servers/client.py'。
- 对于 'single_mcp_server' 示例,运行 'python single_mcp_server/client.py'。
- 对于 'vefaas-function' 示例,在服务器启动后,您可以通过HTTP请求访问FastAPI提供的API接口,例如向 'http://localhost:8000/chat' 发送POST请求并携带问题参数。
关键词 LLM工具, 函数调用, 服务端功能, 外部API集成, 上下文增强
信息
分类
AI与计算