|
| 1 | +// Package mcp provides Model Context Protocol (MCP) server implementation |
| 2 | +// for exposing agent teams to CLI assistants like Gemini CLI and Codex. |
| 3 | +package mcp |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | +) |
| 8 | + |
| 9 | +// JSON-RPC 2.0 types |
| 10 | + |
| 11 | +// Request represents a JSON-RPC 2.0 request. |
| 12 | +type Request struct { |
| 13 | + JSONRPC string `json:"jsonrpc"` |
| 14 | + ID json.RawMessage `json:"id,omitempty"` |
| 15 | + Method string `json:"method"` |
| 16 | + Params json.RawMessage `json:"params,omitempty"` |
| 17 | +} |
| 18 | + |
| 19 | +// Response represents a JSON-RPC 2.0 response. |
| 20 | +type Response struct { |
| 21 | + JSONRPC string `json:"jsonrpc"` |
| 22 | + ID json.RawMessage `json:"id,omitempty"` |
| 23 | + Result interface{} `json:"result,omitempty"` |
| 24 | + Error *ErrorResponse `json:"error,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +// ErrorResponse represents a JSON-RPC 2.0 error. |
| 28 | +type ErrorResponse struct { |
| 29 | + Code int `json:"code"` |
| 30 | + Message string `json:"message"` |
| 31 | + Data interface{} `json:"data,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +// Standard JSON-RPC error codes |
| 35 | +const ( |
| 36 | + ErrParseError = -32700 |
| 37 | + ErrInvalidRequest = -32600 |
| 38 | + ErrMethodNotFound = -32601 |
| 39 | + ErrInvalidParams = -32602 |
| 40 | + ErrInternalError = -32603 |
| 41 | +) |
| 42 | + |
| 43 | +// MCP Protocol types |
| 44 | + |
| 45 | +// ServerInfo represents MCP server information. |
| 46 | +type ServerInfo struct { |
| 47 | + Name string `json:"name"` |
| 48 | + Version string `json:"version"` |
| 49 | +} |
| 50 | + |
| 51 | +// InitializeParams represents the initialize request parameters. |
| 52 | +type InitializeParams struct { |
| 53 | + ProtocolVersion string `json:"protocolVersion"` |
| 54 | + Capabilities Capabilities `json:"capabilities"` |
| 55 | + ClientInfo ClientInfo `json:"clientInfo"` |
| 56 | +} |
| 57 | + |
| 58 | +// ClientInfo represents MCP client information. |
| 59 | +type ClientInfo struct { |
| 60 | + Name string `json:"name"` |
| 61 | + Version string `json:"version"` |
| 62 | +} |
| 63 | + |
| 64 | +// Capabilities represents MCP server capabilities. |
| 65 | +type Capabilities struct { |
| 66 | + Tools *ToolsCapability `json:"tools,omitempty"` |
| 67 | + Resources *ResourcesCapability `json:"resources,omitempty"` |
| 68 | + Prompts *PromptsCapability `json:"prompts,omitempty"` |
| 69 | +} |
| 70 | + |
| 71 | +// ToolsCapability indicates tool support. |
| 72 | +type ToolsCapability struct { |
| 73 | + ListChanged bool `json:"listChanged,omitempty"` |
| 74 | +} |
| 75 | + |
| 76 | +// ResourcesCapability indicates resource support. |
| 77 | +type ResourcesCapability struct { |
| 78 | + Subscribe bool `json:"subscribe,omitempty"` |
| 79 | + ListChanged bool `json:"listChanged,omitempty"` |
| 80 | +} |
| 81 | + |
| 82 | +// PromptsCapability indicates prompt support. |
| 83 | +type PromptsCapability struct { |
| 84 | + ListChanged bool `json:"listChanged,omitempty"` |
| 85 | +} |
| 86 | + |
| 87 | +// InitializeResult represents the initialize response. |
| 88 | +type InitializeResult struct { |
| 89 | + ProtocolVersion string `json:"protocolVersion"` |
| 90 | + Capabilities Capabilities `json:"capabilities"` |
| 91 | + ServerInfo ServerInfo `json:"serverInfo"` |
| 92 | +} |
| 93 | + |
| 94 | +// ToolInfo represents a tool definition. |
| 95 | +type ToolInfo struct { |
| 96 | + Name string `json:"name"` |
| 97 | + Description string `json:"description,omitempty"` |
| 98 | + InputSchema InputSchema `json:"inputSchema"` |
| 99 | +} |
| 100 | + |
| 101 | +// InputSchema represents the JSON Schema for tool input. |
| 102 | +type InputSchema struct { |
| 103 | + Type string `json:"type"` |
| 104 | + Properties map[string]Property `json:"properties,omitempty"` |
| 105 | + Required []string `json:"required,omitempty"` |
| 106 | +} |
| 107 | + |
| 108 | +// Property represents a JSON Schema property. |
| 109 | +type Property struct { |
| 110 | + Type string `json:"type"` |
| 111 | + Description string `json:"description,omitempty"` |
| 112 | + Enum []string `json:"enum,omitempty"` |
| 113 | +} |
| 114 | + |
| 115 | +// ListToolsResult represents the tools/list response. |
| 116 | +type ListToolsResult struct { |
| 117 | + Tools []ToolInfo `json:"tools"` |
| 118 | +} |
| 119 | + |
| 120 | +// CallToolParams represents the tools/call request parameters. |
| 121 | +type CallToolParams struct { |
| 122 | + Name string `json:"name"` |
| 123 | + Arguments map[string]interface{} `json:"arguments,omitempty"` |
| 124 | +} |
| 125 | + |
| 126 | +// CallToolResult represents the tools/call response. |
| 127 | +type CallToolResult struct { |
| 128 | + Content []ContentBlock `json:"content"` |
| 129 | + IsError bool `json:"isError,omitempty"` |
| 130 | +} |
| 131 | + |
| 132 | +// ContentBlock represents a content block in a tool result. |
| 133 | +type ContentBlock struct { |
| 134 | + Type string `json:"type"` // "text" or "image" |
| 135 | + Text string `json:"text,omitempty"` |
| 136 | + // For images (not used in this implementation) |
| 137 | + Data string `json:"data,omitempty"` |
| 138 | + MimeType string `json:"mimeType,omitempty"` |
| 139 | +} |
| 140 | + |
| 141 | +// NewTextContent creates a text content block. |
| 142 | +func NewTextContent(text string) ContentBlock { |
| 143 | + return ContentBlock{ |
| 144 | + Type: "text", |
| 145 | + Text: text, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// NewErrorContent creates an error text content block. |
| 150 | +func NewErrorContent(err error) ContentBlock { |
| 151 | + return ContentBlock{ |
| 152 | + Type: "text", |
| 153 | + Text: "Error: " + err.Error(), |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// ResourceInfo represents a resource definition. |
| 158 | +type ResourceInfo struct { |
| 159 | + URI string `json:"uri"` |
| 160 | + Name string `json:"name"` |
| 161 | + Description string `json:"description,omitempty"` |
| 162 | + MimeType string `json:"mimeType,omitempty"` |
| 163 | +} |
| 164 | + |
| 165 | +// ListResourcesResult represents the resources/list response. |
| 166 | +type ListResourcesResult struct { |
| 167 | + Resources []ResourceInfo `json:"resources"` |
| 168 | +} |
| 169 | + |
| 170 | +// ReadResourceParams represents the resources/read request parameters. |
| 171 | +type ReadResourceParams struct { |
| 172 | + URI string `json:"uri"` |
| 173 | +} |
| 174 | + |
| 175 | +// ReadResourceResult represents the resources/read response. |
| 176 | +type ReadResourceResult struct { |
| 177 | + Contents []ResourceContent `json:"contents"` |
| 178 | +} |
| 179 | + |
| 180 | +// ResourceContent represents resource content. |
| 181 | +type ResourceContent struct { |
| 182 | + URI string `json:"uri"` |
| 183 | + MimeType string `json:"mimeType,omitempty"` |
| 184 | + Text string `json:"text,omitempty"` |
| 185 | + Blob string `json:"blob,omitempty"` // Base64 encoded |
| 186 | +} |
| 187 | + |
| 188 | +// PromptInfo represents a prompt definition. |
| 189 | +type PromptInfo struct { |
| 190 | + Name string `json:"name"` |
| 191 | + Description string `json:"description,omitempty"` |
| 192 | + Arguments []PromptArgument `json:"arguments,omitempty"` |
| 193 | +} |
| 194 | + |
| 195 | +// PromptArgument represents a prompt argument. |
| 196 | +type PromptArgument struct { |
| 197 | + Name string `json:"name"` |
| 198 | + Description string `json:"description,omitempty"` |
| 199 | + Required bool `json:"required,omitempty"` |
| 200 | +} |
| 201 | + |
| 202 | +// ListPromptsResult represents the prompts/list response. |
| 203 | +type ListPromptsResult struct { |
| 204 | + Prompts []PromptInfo `json:"prompts"` |
| 205 | +} |
| 206 | + |
| 207 | +// GetPromptParams represents the prompts/get request parameters. |
| 208 | +type GetPromptParams struct { |
| 209 | + Name string `json:"name"` |
| 210 | + Arguments map[string]string `json:"arguments,omitempty"` |
| 211 | +} |
| 212 | + |
| 213 | +// GetPromptResult represents the prompts/get response. |
| 214 | +type GetPromptResult struct { |
| 215 | + Description string `json:"description,omitempty"` |
| 216 | + Messages []PromptMessage `json:"messages"` |
| 217 | +} |
| 218 | + |
| 219 | +// PromptMessage represents a message in a prompt. |
| 220 | +type PromptMessage struct { |
| 221 | + Role string `json:"role"` // "user" or "assistant" |
| 222 | + Content ContentBlock `json:"content"` |
| 223 | +} |
| 224 | + |
| 225 | +// Notification types |
| 226 | + |
| 227 | +// Notification represents a JSON-RPC notification (no response expected). |
| 228 | +type Notification struct { |
| 229 | + JSONRPC string `json:"jsonrpc"` |
| 230 | + Method string `json:"method"` |
| 231 | + Params json.RawMessage `json:"params,omitempty"` |
| 232 | +} |
| 233 | + |
| 234 | +// ProgressParams represents progress notification parameters. |
| 235 | +type ProgressParams struct { |
| 236 | + ProgressToken interface{} `json:"progressToken"` |
| 237 | + Progress float64 `json:"progress"` |
| 238 | + Total float64 `json:"total,omitempty"` |
| 239 | +} |
0 commit comments