feat(mcp): jsonrpc envelope types and error codes

This commit is contained in:
Mathias Bergqvist
2026-05-04 20:40:29 +02:00
parent 87cbce27a1
commit abaf5e8b1d
2 changed files with 69 additions and 0 deletions

43
internal/mcp/jsonrpc.go Normal file
View File

@@ -0,0 +1,43 @@
package mcp
import "encoding/json"
const (
CodePermissionDenied = -32001
CodeNotFound = -32002
CodeConflict = -32003
CodeValidation = -32004
CodeUpstreamGitea = -32005
)
type Request struct {
JSONRPC string `json:"jsonrpc"`
ID any `json:"id,omitempty"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
type Response struct {
JSONRPC string `json:"jsonrpc"`
ID any `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"`
}
func NewResponse(id any, result any) Response {
return Response{JSONRPC: "2.0", ID: id, Result: result}
}
func NewErrorResponse(id any, code int, msg string, data any) Response {
return Response{
JSONRPC: "2.0",
ID: id,
Error: &RPCError{Code: code, Message: msg, Data: data},
}
}