# 🔄 AgentAPI Proxy 配置指南 ## ✅ SSE 服务器已启动 **Funstat MCP SSE 服务器**: - ✅ 已成功转换为 SSE 模式 - ✅ 运行中(PID: 83898) - 📡 SSE 端点: `http://127.0.0.1:8091/sse` - 📨 消息端点: `http://127.0.0.1:8091/messages` --- ## 🔌 使用 AgentAPI Proxy 连接 ### 方法 1: 命令行直接使用 ```bash # 启动 agentapi proxy,连接到 funstat SSE 服务器 /Users/lucas/牛马/agentapi proxy http://127.0.0.1:8091/sse ``` 这将创建一个 **STDIO ↔ SSE 代理**,Claude Code 可以通过标准输入输出与 funstat 通信。 ### 方法 2: 通过 claude-code-mcp-config.json 配置 修改项目配置文件: **文件**: `/Users/lucas/chat--1003255561049/claude-code-mcp-config.json` ```json { "funstat": { "command": "/Users/lucas/牛马/agentapi", "args": ["proxy", "http://127.0.0.1:8091/sse"], "env": {} } } ``` ### 方法 3: 修改 AgentAPI 配置(推荐) 如果 agentapi 支持配置多个 MCP 服务器,可以在配置中添加: **文件**: `/Users/lucas/牛马/config.json` 添加 MCP 服务器列表(如果支持): ```json { "mcp_servers": { "funstat": { "type": "sse", "url": "http://127.0.0.1:8091/sse" } } } ``` --- ## 🧪 测试 Proxy 连接 ### 测试 1: 手动运行 Proxy ```bash # 在终端运行 /Users/lucas/牛马/agentapi proxy http://127.0.0.1:8091/sse ``` 成功时应该看到: ``` ✅ 连接到 SSE 服务器 ✅ STDIO 代理已启动 ``` ### 测试 2: 使用 Python MCP 客户端 ```python import subprocess import json # 启动 proxy 作为子进程 proxy = subprocess.Popen( ['/Users/lucas/牛马/agentapi', 'proxy', 'http://127.0.0.1:8091/sse'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) # 发送 MCP 请求 request = { "jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {} } proxy.stdin.write((json.dumps(request) + '\\n').encode()) proxy.stdin.flush() # 读取响应 response = proxy.stdout.readline() print(response.decode()) ``` --- ## 📊 架构图 ``` ┌──────────────────┐ │ Claude Code │ │ (STDIO 客户端) │ └────────┬─────────┘ │ STDIO (stdin/stdout) ▼ ┌──────────────────┐ │ agentapi proxy │ │ (STDIO ↔ SSE) │ └────────┬─────────┘ │ HTTP SSE ▼ ┌──────────────────┐ │ Funstat SSE │ │ MCP Server │ │ :8091 │ └────────┬─────────┘ │ Telethon ▼ ┌──────────────────┐ │ @openaiw_bot │ │ (Telegram BOT) │ └──────────────────┘ ``` --- ## 🚀 自动启动配置 ### 创建启动脚本 ```bash #!/bin/bash # start_funstat_mcp.sh # 1. 启动 SSE 服务器 cd /Users/lucas/chat--1003255561049/funstat_mcp nohup python3 server.py > funstat_sse.log 2>&1 & SSE_PID=$! echo "✅ SSE 服务器已启动 (PID: $SSE_PID)" # 等待服务器就绪 sleep 5 # 2. 验证服务器 curl -I http://127.0.0.1:8091/sse 2>&1 | grep "200 OK" && echo "✅ SSE 服务器响应正常" || echo "❌ SSE 服务器无响应" echo "" echo "📡 Funstat MCP SSE 已启动" echo " SSE 端点: http://127.0.0.1:8091/sse" echo "" echo "🔗 通过 agentapi proxy 连接:" echo " /Users/lucas/牛马/agentapi proxy http://127.0.0.1:8091/sse" ``` ### 添加到系统启动(可选) 使用 launchd 或 systemd 在系统启动时自动运行。 --- ## 🔍 故障排查 ### 问题 1: SSE 服务器未运行 **检查**: ```bash ps aux | grep "python3 server.py" lsof -i :8091 ``` **解决**: ```bash cd /Users/lucas/chat--1003255561049/funstat_mcp ./start_sse.sh ``` ### 问题 2: Proxy 连接失败 **检查**: ```bash curl -I http://127.0.0.1:8091/sse ``` 应该返回 `HTTP/1.1 200 OK` **解决**: - 确保 SSE 服务器正在运行 - 检查防火墙设置 - 查看日志: `tail -f funstat_sse.log` ### 问题 3: Session 过期 **症状**: Telegram 连接失败 **解决**: ```bash cd /Users/lucas/chat--1003255561049 python3 create_session_safe.py ``` --- ## 📝 配置文件示例 ### claude-code-mcp-config.json(项目级) ```json { "funstat": { "command": "/Users/lucas/牛马/agentapi", "args": ["proxy", "http://127.0.0.1:8091/sse"], "env": {} } } ``` ### Claude Desktop Config(全局) **文件**: `~/Library/Application Support/Claude/claude_desktop_config.json` ```json { "mcpServers": { "funstat": { "command": "/Users/lucas/牛马/agentapi", "args": ["proxy", "http://127.0.0.1:8091/sse"] } } } ``` --- ## ✅ 验证清单 - [ ] SSE 服务器已启动(端口 8091) - [ ] SSE 端点响应正常(HTTP 200) - [ ] Telegram 连接正常 - [ ] AgentAPI 已安装 - [ ] Proxy 命令可执行 - [ ] 配置文件已更新 - [ ] Claude Code 已重启 --- ## 🎯 下一步 1. **测试 Proxy 连接**: ```bash /Users/lucas/牛马/agentapi proxy http://127.0.0.1:8091/sse ``` 2. **更新配置文件** (根据需要选择上述方法之一) 3. **重启 Claude Code** 或 AgentAPI 4. **验证工具可用性** - 应该能看到 8 个 funstat 工具 --- ## 📄 相关文件 - SSE 服务器: `/Users/lucas/chat--1003255561049/funstat_mcp/server.py` - 启动脚本: `/Users/lucas/chat--1003255561049/funstat_mcp/start_sse.sh` - 日志文件: `/Users/lucas/chat--1003255561049/funstat_mcp/funstat_sse.log` - AgentAPI: `/Users/lucas/牛马/agentapi` - Session: `~/telegram_sessions/funstat_bot.session` --- *文档创建时间: 2025-10-26 21:28* *SSE 服务器状态: ✅ 运行中* *端口: 8091*