45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package mcp
|
|
|
|
import "encoding/json"
|
|
|
|
// JSON-RPC 2.0 envelope types (the wire format MCP rides on). Notifications are
|
|
// requests with no id and must not receive a response.
|
|
|
|
type rpcRequest struct {
|
|
JSONRPC string `json:"jsonrpc"`
|
|
ID json.RawMessage `json:"id,omitempty"`
|
|
Method string `json:"method"`
|
|
Params json.RawMessage `json:"params,omitempty"`
|
|
}
|
|
|
|
func (r *rpcRequest) isNotification() bool { return len(r.ID) == 0 }
|
|
|
|
type rpcResponse struct {
|
|
JSONRPC string `json:"jsonrpc"`
|
|
ID json.RawMessage `json:"id,omitempty"`
|
|
Result any `json:"result,omitempty"`
|
|
Error *rpcError `json:"error,omitempty"`
|
|
}
|
|
|
|
type rpcError struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data any `json:"data,omitempty"`
|
|
}
|
|
|
|
const (
|
|
codeParseError = -32700
|
|
codeInvalidRequest = -32600
|
|
codeMethodNotFound = -32601
|
|
codeInvalidParams = -32602
|
|
codeInternalError = -32603
|
|
)
|
|
|
|
func result(id json.RawMessage, v any) *rpcResponse {
|
|
return &rpcResponse{JSONRPC: "2.0", ID: id, Result: v}
|
|
}
|
|
|
|
func errorResponse(id json.RawMessage, code int, msg string) *rpcResponse {
|
|
return &rpcResponse{JSONRPC: "2.0", ID: id, Error: &rpcError{Code: code, Message: msg}}
|
|
}
|