Fix the problem that mcp request id is not of int type (#4914)

This commit is contained in:
MarkJoyMa
2025-06-07 10:37:18 +08:00
committed by GitHub
parent 4b2095ed03
commit d4cccca387
3 changed files with 120 additions and 31 deletions

View File

@@ -3,6 +3,7 @@ package mcp
import (
"context"
"encoding/json"
"fmt"
"sync"
"github.com/zeromicro/go-zero/rest"
@@ -15,11 +16,31 @@ type Cursor string
type Request struct {
SessionId string `form:"session_id"` // Session identifier for client tracking
JsonRpc string `json:"jsonrpc"` // Must be "2.0" per JSON-RPC spec
ID int64 `json:"id"` // Request identifier for matching responses
ID any `json:"id"` // Request identifier for matching responses
Method string `json:"method"` // Method name to invoke
Params json.RawMessage `json:"params"` // Parameters for the method
}
func (r Request) isNotification() (bool, error) {
var isNotification bool
switch val := r.ID.(type) {
case int:
isNotification = val == 0
case int64:
isNotification = val == 0
case float64:
isNotification = val == 0.0
case string:
isNotification = len(val) == 0
case nil:
isNotification = true
default:
return false, fmt.Errorf("invalid type %T", val)
}
return isNotification, nil
}
type PaginatedParams struct {
Cursor string `json:"cursor"`
Meta struct {
@@ -244,7 +265,7 @@ type errorObj struct {
// Response represents a JSON-RPC response
type Response struct {
JsonRpc string `json:"jsonrpc"` // Always "2.0"
ID int64 `json:"id"` // Same as request ID
ID any `json:"id"` // Same as request ID
Result any `json:"result"` // Result object (null if error)
Error *errorObj `json:"error,omitempty"` // Error object (null if success)
}