Files
gitea-mcp/internal/mcp/jsonrpc.go
2026-05-04 20:46:07 +02:00

60 lines
1.7 KiB
Go

package mcp
import "encoding/json"
// JSON-RPC application-defined error codes (range -32000 to -32099 per spec).
// Tool handlers return one of these from tools/call to signal a typed failure.
const (
// CodePermissionDenied: caller authenticated but lacks permission for this
// resource (e.g. owner not in the allowlist).
CodePermissionDenied = -32001
// CodeNotFound: target repo, file, branch, PR, issue, or workflow run does
// not exist.
CodeNotFound = -32002
// CodeConflict: write attempted on stale state (branch already exists,
// non-fast-forward push, file modified concurrently).
CodeConflict = -32003
// CodeValidation: arguments failed input validation (bad regex, oversized
// payload, missing required field).
CodeValidation = -32004
// CodeUpstreamGitea: Gitea API returned an error this server could not map
// to one of the codes above. The original status is in error.data.
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},
}
}