protocol.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package mcp
  2. import "encoding/json"
  3. // MCP Protocol constants
  4. const (
  5. ProtocolVersion = "2024-11-05"
  6. ContentType = "application/json"
  7. )
  8. // JSON-RPC 2.0 types
  9. type JSONRPCRequest struct {
  10. JSONRPC string `json:"jsonrpc"`
  11. ID interface{} `json:"id,omitempty"`
  12. Method string `json:"method"`
  13. Params json.RawMessage `json:"params,omitempty"`
  14. }
  15. type JSONRPCResponse struct {
  16. JSONRPC string `json:"jsonrpc"`
  17. ID interface{} `json:"id,omitempty"`
  18. Result interface{} `json:"result,omitempty"`
  19. Error *RPCError `json:"error,omitempty"`
  20. }
  21. type RPCError struct {
  22. Code int `json:"code"`
  23. Message string `json:"message"`
  24. Data interface{} `json:"data,omitempty"`
  25. }
  26. // Standard JSON-RPC error codes
  27. var (
  28. ErrParseError = &RPCError{Code: -32700, Message: "Parse error"}
  29. ErrInvalidRequest = &RPCError{Code: -32600, Message: "Invalid request"}
  30. ErrMethodNotFound = &RPCError{Code: -32601, Message: "Method not found"}
  31. ErrInvalidParams = &RPCError{Code: -32602, Message: "Invalid params"}
  32. ErrInternal = &RPCError{Code: -32603, Message: "Internal error"}
  33. )
  34. // MCP specific types
  35. type InitializeParams struct {
  36. ProtocolVersion string `json:"protocolVersion"`
  37. Capabilities ClientCapabilities `json:"capabilities"`
  38. ClientInfo ImplementationInfo `json:"clientInfo"`
  39. Meta map[string]interface{} `json:"_meta,omitempty"`
  40. }
  41. type InitializeResult struct {
  42. ProtocolVersion string `json:"protocolVersion"`
  43. Capabilities ServerCapabilities `json:"capabilities"`
  44. ServerInfo ImplementationInfo `json:"serverInfo"`
  45. Instructions string `json:"instructions,omitempty"`
  46. }
  47. type ClientCapabilities struct {
  48. Experimental map[string]interface{} `json:"experimental,omitempty"`
  49. Roots *RootsCapability `json:"roots,omitempty"`
  50. Sampling *SamplingCapability `json:"sampling,omitempty"`
  51. }
  52. type RootsCapability struct {
  53. ListChanged bool `json:"listChanged,omitempty"`
  54. }
  55. type SamplingCapability struct{}
  56. type ServerCapabilities struct {
  57. Experimental map[string]interface{} `json:"experimental,omitempty"`
  58. Tools *ToolsCapability `json:"tools,omitempty"`
  59. Resources *ResourcesCapability `json:"resources,omitempty"`
  60. }
  61. type ToolsCapability struct {
  62. ListChanged bool `json:"listChanged,omitempty"`
  63. }
  64. type ResourcesCapability struct {
  65. Subscribe bool `json:"subscribe,omitempty"`
  66. ListChanged bool `json:"listChanged,omitempty"`
  67. }
  68. type ImplementationInfo struct {
  69. Name string `json:"name"`
  70. Version string `json:"version"`
  71. }
  72. // Tool types
  73. type Tool struct {
  74. Name string `json:"name"`
  75. Description string `json:"description"`
  76. InputSchema InputSchema `json:"inputSchema"`
  77. }
  78. type InputSchema struct {
  79. Type string `json:"type"`
  80. Properties map[string]Property `json:"properties,omitempty"`
  81. Required []string `json:"required,omitempty"`
  82. AdditionalProperties bool `json:"additionalProperties"`
  83. }
  84. type Property struct {
  85. Type string `json:"type"`
  86. Description string `json:"description,omitempty"`
  87. }
  88. type ListToolsResult struct {
  89. Tools []Tool `json:"tools"`
  90. }
  91. type CallToolParams struct {
  92. Name string `json:"name"`
  93. Arguments map[string]interface{} `json:"arguments,omitempty"`
  94. }
  95. type CallToolResult struct {
  96. Content []ContentBlock `json:"content"`
  97. IsError bool `json:"isError,omitempty"`
  98. }
  99. type ContentBlock struct {
  100. Type string `json:"type"`
  101. Text string `json:"text"`
  102. }
  103. // Ping
  104. type PingResult struct{}
  105. // Resource types for subscriptions
  106. type Resource struct {
  107. URI string `json:"uri"`
  108. Name string `json:"name"`
  109. Description string `json:"description,omitempty"`
  110. MimeType string `json:"mimeType,omitempty"`
  111. }
  112. type ListResourcesResult struct {
  113. Resources []Resource `json:"resources"`
  114. }
  115. type ReadResourceParams struct {
  116. URI string `json:"uri"`
  117. }
  118. type ReadResourceResult struct {
  119. Contents []ResourceContents `json:"contents"`
  120. }
  121. type ResourceContents struct {
  122. URI string `json:"uri"`
  123. MimeType string `json:"mimeType,omitempty"`
  124. Text string `json:"text,omitempty"`
  125. Blob string `json:"blob,omitempty"` // base64 encoded
  126. }
  127. // Subscription types
  128. type SubscribeParams struct {
  129. URI string `json:"uri"`
  130. }
  131. type UnsubscribeParams struct {
  132. URI string `json:"uri"`
  133. }
  134. // Resource update notification
  135. type ResourceUpdatedNotification struct {
  136. URI string `json:"uri"`
  137. Contents ResourceContents `json:"contents"`
  138. }
  139. // JSON-RPC Notification
  140. type JSONRPCNotification struct {
  141. JSONRPC string `json:"jsonrpc"`
  142. Method string `json:"method"`
  143. Params interface{} `json:"params,omitempty"`
  144. }
  145. // CreateResourceNotification creates a JSON-RPC notification for resource updates
  146. func CreateResourceNotification(uri string, data interface{}) *JSONRPCNotification {
  147. // Marshal the data to JSON
  148. dataBytes, err := json.Marshal(data)
  149. if err != nil {
  150. dataBytes = []byte("{}")
  151. }
  152. return &JSONRPCNotification{
  153. JSONRPC: "2.0",
  154. Method: "notifications/resources/updated",
  155. Params: ResourceUpdatedNotification{
  156. URI: uri,
  157. Contents: ResourceContents{
  158. URI: uri,
  159. MimeType: "application/json",
  160. Text: string(dataBytes),
  161. },
  162. },
  163. }
  164. }