60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / mcp / types.go
3.2 KB
1// Package mcp provides MCP (Model Context Protocol) client and proxy functionality. 2package mcp 3 4import ( 5 "context" 6 "encoding/json" 7) 8 9// Request represents an MCP request. 10type Request struct { 11 JSONRPC string `json:"jsonrpc"` 12 ID interface{} `json:"id,omitempty"` 13 Method string `json:"method"` 14 Params json.RawMessage `json:"params,omitempty"` 15} 16 17// Response represents an MCP response. 18type Response struct { 19 JSONRPC string `json:"jsonrpc"` 20 ID interface{} `json:"id,omitempty"` 21 Result json.RawMessage `json:"result,omitempty"` 22 Error *Error `json:"error,omitempty"` 23} 24 25// Error represents an MCP error. 26type Error struct { 27 Code int `json:"code"` 28 Message string `json:"message"` 29} 30 31// Tool represents an MCP tool definition. 32type Tool struct { 33 Name string `json:"name"` 34 Description string `json:"description"` 35 InputSchema json.RawMessage `json:"inputSchema"` 36} 37 38// ToolCall represents a tool invocation. 39type ToolCall struct { 40 Name string `json:"name"` 41 Arguments json.RawMessage `json:"arguments"` 42} 43 44// ToolResult represents the result of a tool call. 45type ToolResult struct { 46 Content []ContentBlock `json:"content"` 47 IsError bool `json:"isError,omitempty"` 48} 49 50// ContentBlock represents a content block in tool results. 51type ContentBlock struct { 52 Type string `json:"type"` 53 Text string `json:"text,omitempty"` 54} 55 56// ListToolsResult is the result of tools/list. 57type ListToolsResult struct { 58 Tools []Tool `json:"tools"` 59} 60 61// CallToolParams are parameters for tools/call. 62type CallToolParams struct { 63 Name string `json:"name"` 64 Arguments json.RawMessage `json:"arguments"` 65} 66 67// NewRequest creates a new MCP request. 68func NewRequest(id interface{}, method string, params interface{}) (*Request, error) { 69 var paramsJSON json.RawMessage 70 if params != nil { 71 data, err := json.Marshal(params) 72 if err != nil { 73 return nil, err 74 } 75 paramsJSON = data 76 } 77 78 return &Request{ 79 JSONRPC: "2.0", 80 ID: id, 81 Method: method, 82 Params: paramsJSON, 83 }, nil 84} 85 86// NewResponse creates a new MCP response. 87func NewResponse(id interface{}, result interface{}) (*Response, error) { 88 var resultJSON json.RawMessage 89 if result != nil { 90 data, err := json.Marshal(result) 91 if err != nil { 92 return nil, err 93 } 94 resultJSON = data 95 } 96 97 return &Response{ 98 JSONRPC: "2.0", 99 ID: id, 100 Result: resultJSON, 101 }, nil 102} 103 104// NewErrorResponse creates a new MCP error response. 105func NewErrorResponse(id interface{}, code int, message string) *Response { 106 return &Response{ 107 JSONRPC: "2.0", 108 ID: id, 109 Error: &Error{ 110 Code: code, 111 Message: message, 112 }, 113 } 114} 115 116// Standard MCP error codes 117const ( 118 ErrorCodeParseError = -32700 119 ErrorCodeInvalidRequest = -32600 120 ErrorCodeMethodNotFound = -32601 121 ErrorCodeInvalidParams = -32602 122 ErrorCodeInternalError = -32603 123) 124 125// UpstreamClient defines the interface for an upstream MCP server connection. 126type UpstreamClient interface { 127 Initialize(ctx context.Context, clientInfo map[string]interface{}) (*Response, error) 128 ListTools(ctx context.Context) ([]Tool, error) 129 CallTool(ctx context.Context, name string, arguments interface{}) (*ToolResult, error) 130}