MCPcopy Create free account
hub / github.com/astercloud/aster / HandleJSONRPC

Method HandleJSONRPC

pkg/a2a/handler.go:63–133  ·  view source on GitHub ↗

HandleJSONRPC 处理 JSON-RPC 2.0 请求 POST /a2a/{agentId}

(c *gin.Context)

Source from the content-addressed store, hash-verified

61// HandleJSONRPC 处理 JSON-RPC 2.0 请求
62// POST /a2a/{agentId}
63func (h *Handler) HandleJSONRPC(c *gin.Context) {
64 ctx := c.Request.Context()
65 agentID := c.Param("agentId")
66
67 if agentID == "" {
68 c.JSON(http.StatusBadRequest, gin.H{
69 "success": false,
70 "error": gin.H{
71 "code": "bad_request",
72 "message": "Missing agentId parameter",
73 },
74 })
75 return
76 }
77
78 // 解析 JSON-RPC 请求
79 var req JSONRPCRequest
80 if err := c.ShouldBindJSON(&req); err != nil {
81 // 返回 JSON-RPC 错误响应
82 c.JSON(http.StatusBadRequest, &JSONRPCResponse{
83 JSONRPC: "2.0",
84 ID: nil, // 解析错误时 ID 可能为 nil
85 Error: &RPCError{
86 Code: -32700, // Parse error
87 Message: "Invalid JSON was received",
88 Data: err.Error(),
89 },
90 })
91 return
92 }
93
94 logging.Info(ctx, "a2a.jsonrpc_request", map[string]any{
95 "agent_id": agentID,
96 "method": req.Method,
97 "id": req.ID,
98 })
99
100 // 验证 JSON-RPC 版本
101 if req.JSONRPC != "2.0" {
102 c.JSON(http.StatusBadRequest, &JSONRPCResponse{
103 JSONRPC: "2.0",
104 ID: req.ID,
105 Error: &RPCError{
106 Code: -32600, // Invalid Request
107 Message: "Invalid JSON-RPC version, must be '2.0'",
108 },
109 })
110 return
111 }
112
113 // 处理请求
114 resp := h.server.HandleRequest(ctx, agentID, &req)
115
116 // 记录响应
117 if resp.Error != nil {
118 logging.Warn(ctx, "a2a.jsonrpc_error", map[string]any{
119 "agent_id": agentID,
120 "method": req.Method,

Callers

nothing calls this directly

Calls 6

InfoFunction · 0.92
WarnFunction · 0.92
JSONMethod · 0.80
HandleRequestMethod · 0.80
ContextMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected