MCP聊天应用程序
一个使用流式HTTP传输(SSE)演示模型上下文协议(MCP)概念的全栈聊天应用程序。使用React(前端)和Python FastAPI(后端)构建,OpenRouter作为LLM提供者。
特性
- MCP实施:基于HTTP的JSON-RPC 2.0,支持SSE流媒体
- 工具支持:演示使用天气工具调用MCP工具
- 流媒体响应:来自LLM的实时令牌流
- 现代用户界面:干净、响应迅速的聊天界面
- OpenRouter集成:使用具有可配置模型的OpenRouter API
建筑
┌─────────────────┐ HTTP POST ┌─────────────────────┐
│ React │ ──────────────────>│ FastAPI Backend │
│ Frontend │ (user message) │ (MCP Server) │
│ │ str:
return "my_tool"
@property
def description(self) -> str:
return "Description of my tool"
@property
def input_schema(self) -> dict:
return {
"type": "object",
"properties": {
"param": {"type": "string"}
},
"required": ["param"]
}
async def execute(self, **kwargs):
return {"result": "Tool executed"}- 在中注册
app/mcp/server.py:
from app.tools.my_tool import MyTool
# In _register_default_tools:
self.register_tool(MyTool())更改LLM模型
编辑 backend/app/config.py:
DEFAULT_MODEL: str = "anthropic/claude-3-haiku" # Or any OpenRouter model许可证
麻省理工学院
