Handler 返回一个 http.Handler, 处理 MCP JSON-RPC 请求。 典型挂载方式: mux.Handle("/mcp", mcpSrv.Handler())
()
| 63 | // |
| 64 | // mux.Handle("/mcp", mcpSrv.Handler()) |
| 65 | func (s *Server) Handler() http.Handler { |
| 66 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 67 | if r.Method != http.MethodPost { |
| 68 | http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | var req cloud.MCPRequest |
| 73 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 74 | writeError(w, req.ID, -32700, "invalid JSON", err) |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | switch req.Method { |
| 79 | case "tools/list": |
| 80 | s.handleToolsList(w, r.Context(), &req) |
| 81 | case "tools/call": |
| 82 | s.handleToolsCall(w, r.Context(), &req) |
| 83 | default: |
| 84 | writeError(w, req.ID, -32601, "method not found", nil) |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | // handleToolsList 处理 tools/list 请求 |
| 90 | func (s *Server) handleToolsList(w http.ResponseWriter, ctx context.Context, req *cloud.MCPRequest) { |
no test coverage detected