MCP 服务器
项目概述
该项目基于 FastAPI 和 MCP(模型上下文协议)构建,实现了 AI 模型与开发环境之间标准化的上下文交互。通过简化模型部署、提供高效的 API 端点以及确保模型输入输出的一致性,增强了 AI 应用的可扩展性和可维护性,使开发者能够更轻松地集成和管理 AI 任务。
MCP(模型上下文协议)是 AI 模型与开发环境之间上下文交互的统一协议。本项目提供基于 Python 的 MCP 服务器实现,支持 MCP 协议的基本功能,包括初始化、采样和会话管理。
特征
- JSON-RPC 2.0 :基于标准 JSON-RPC 2.0 协议的请求-响应通信
- SSE 连接:支持服务器发送事件连接以实现实时通知
- 模块化设计:模块化架构,易于扩展和定制
- 异步处理:使用 FastAPI 和异步 IO 的高性能服务
- 完整客户端:包括完整的测试客户端实现
项目结构
安装
- 克隆存储库:
- 安装依赖项:
用法
启动服务器
默认情况下,服务器将从127.0.0.1:12000
启动。您可以使用环境变量自定义主机和端口:
运行客户端
在另一个终端运行客户端:
如果服务器没有在默认地址运行,可以设置环境变量:
API 端点
服务器提供以下 API 端点:
- 根路径(
/
):提供服务器信息 - API 端点(
/api
):处理 JSON-RPC 请求 - SSE 端点(
/sse
):处理 SSE 连接
MCP 协议实现
初始化流程
- 客户端通过 SSE 连接到服务器
- 服务器返回 API 端点 URI
- 客户端发送带有协议版本和功能的初始化请求
- 服务器响应初始化请求,返回服务器能力
采样请求
客户可以发送带有提示的采样请求:
服务器将返回采样结果:
关闭会话
客户端可以发送关机请求:
服务器将正常关闭:
开发扩展
添加新方法
要添加新的 MCP 方法,请向MCPServer
类添加一个处理函数,并在_register_methods
方法中注册它:
整合人工智能模型
要集成实际的 AI 模型,请修改handle_sample
方法:
故障排除
常见问题
- 连接错误:确保服务器正在运行,并且客户端正在使用正确的服务器 URL
- 405 方法不允许:确保客户端将请求发送到正确的 API 端点
- SSE 连接失败:检查网络连接和防火墙设置
日志记录
服务器和客户端均提供详细的日志记录。查看日志以获取更多信息:
参考
执照
本项目遵循 MIT 许可证。详情请参阅 LICENSE 文件。
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
基于 FastAPI 实现的模型上下文协议,可以实现 AI 模型与开发环境的标准化交互,方便开发者更轻松地集成和管理 AI 任务。
Related MCP Servers
- AsecurityAlicenseAqualityThis server implements the Model Context Protocol to facilitate meaningful interaction and understanding development between humans and AI through structured tools and progressive interaction patterns.Last updated -1322TypeScriptMIT License
- -securityFlicense-qualityA Model Context Protocol server that provides persistent task management capabilities for AI assistants, allowing them to create, update, and track tasks beyond their usual context limitations.Last updated -1TypeScript
- -securityFlicense-qualityA demonstration implementation of the Model Context Protocol server that facilitates communication between AI models and external tools while maintaining context awareness.Last updated -Python
- -securityAlicense-qualityA zero-configuration tool that automatically converts FastAPI endpoints into Model Context Protocol (MCP) tools, enabling AI systems to interact with your API through natural language.Last updated -1PythonMIT License