@@ -24,14 +24,14 @@ const DefaultProtocolVersion = "2025-03-26"
2424// It handles the MCP handshake and dispatches tools, resources, and prompts
2525// to registered handlers.
2626type Server struct {
27- name string
28- version string
29- protocolVer string
30- tools map [string ]Tool
31- resources map [string ]Resource
32- prompts map [string ]Prompt
33- initialized bool
34- mu sync.Mutex
27+ name string
28+ version string
29+ protocolVer string
30+ tools map [string ]Tool
31+ resources map [string ]Resource
32+ prompts map [string ]Prompt
33+ initialized bool
34+ mu sync.Mutex
3535}
3636
3737// NewServer creates a new MCP server with the given name and version.
@@ -111,7 +111,7 @@ func (s *Server) RunWithIO(r io.Reader, w io.Writer) error {
111111 respErr = encoder .Encode (JSONRPCResponse {
112112 JSONRPC : "2.0" ,
113113 ID : req .ID ,
114- Result : map [ string ] any {},
114+ Result : emptyObject {},
115115 })
116116 case "tools/list" :
117117 respErr = s .handleToolsList (req , encoder )
@@ -148,16 +148,11 @@ func (s *Server) handleInitialize(req JSONRPCRequest, encoder *json.Encoder) err
148148 resp := JSONRPCResponse {
149149 JSONRPC : "2.0" ,
150150 ID : req .ID ,
151- Result : map [string ]any {
152- "protocolVersion" : ver ,
153- "serverInfo" : map [string ]any {
154- "name" : s .name ,
155- "version" : s .version ,
156- },
157- "capabilities" : map [string ]any {
158- "tools" : map [string ]any {},
159- "resources" : map [string ]any {},
160- "prompts" : map [string ]any {},
151+ Result : initializeResult {
152+ ProtocolVersion : ver ,
153+ ServerInfo : serverInfo {
154+ Name : s .name ,
155+ Version : s .version ,
161156 },
162157 },
163158 }
@@ -180,9 +175,7 @@ func (s *Server) handleToolsList(req JSONRPCRequest, encoder *json.Encoder) erro
180175 resp := JSONRPCResponse {
181176 JSONRPC : "2.0" ,
182177 ID : req .ID ,
183- Result : map [string ]any {
184- "tools" : toolList ,
185- },
178+ Result : toolsListResult {Tools : toolList },
186179 }
187180 return encoder .Encode (resp )
188181}
@@ -214,11 +207,11 @@ func (s *Server) handleToolsCall(req JSONRPCRequest, encoder *json.Encoder) erro
214207 resp := JSONRPCResponse {
215208 JSONRPC : "2.0" ,
216209 ID : req .ID ,
217- Result : map [ string ] any {
218- "content" : []map [ string ] any {
219- {"type" : "text" , "text" : fmt .Sprintf ("Unknown tool: %s" , params .Name )},
210+ Result : toolCallResult {
211+ Content : []textContent {
212+ {Type : "text" , Text : fmt .Sprintf ("Unknown tool: %s" , params .Name )},
220213 },
221- "isError" : true ,
214+ IsError : true ,
222215 },
223216 }
224217 return encoder .Encode (resp )
@@ -231,11 +224,11 @@ func (s *Server) handleToolsCall(req JSONRPCRequest, encoder *json.Encoder) erro
231224 resp := JSONRPCResponse {
232225 JSONRPC : "2.0" ,
233226 ID : req .ID ,
234- Result : map [ string ] any {
235- "content" : []map [ string ] any {
236- {"type" : "text" , "text" : fmt .Sprintf ("Error: %v" , err )},
227+ Result : toolCallResult {
228+ Content : []textContent {
229+ {Type : "text" , Text : fmt .Sprintf ("Error: %v" , err )},
237230 },
238- "isError" : true ,
231+ IsError : true ,
239232 },
240233 }
241234 return encoder .Encode (resp )
@@ -244,12 +237,9 @@ func (s *Server) handleToolsCall(req JSONRPCRequest, encoder *json.Encoder) erro
244237 resp := JSONRPCResponse {
245238 JSONRPC : "2.0" ,
246239 ID : req .ID ,
247- Result : map [string ]any {
248- "content" : []map [string ]any {
249- {
250- "type" : "text" ,
251- "text" : result ,
252- },
240+ Result : toolCallResult {
241+ Content : []textContent {
242+ {Type : "text" , Text : result },
253243 },
254244 },
255245 }
@@ -265,9 +255,7 @@ func (s *Server) handleResourcesList(req JSONRPCRequest, encoder *json.Encoder)
265255 resp := JSONRPCResponse {
266256 JSONRPC : "2.0" ,
267257 ID : req .ID ,
268- Result : map [string ]any {
269- "resources" : resourceList ,
270- },
258+ Result : resourcesListResult {Resources : resourceList },
271259 }
272260 return encoder .Encode (resp )
273261}
@@ -298,12 +286,12 @@ func (s *Server) handleResourcesRead(req JSONRPCRequest, encoder *json.Encoder)
298286 resp := JSONRPCResponse {
299287 JSONRPC : "2.0" ,
300288 ID : req .ID ,
301- Result : map [ string ] any {
302- "contents" : []map [ string ] any {
289+ Result : resourcesReadResult {
290+ Contents : []resourceContent {
303291 {
304- "uri" : res .URI ,
305- "mimeType" : res .MimeType ,
306- "text" : content ,
292+ URI : res .URI ,
293+ MimeType : res .MimeType ,
294+ Text : content ,
307295 },
308296 },
309297 },
@@ -320,9 +308,7 @@ func (s *Server) handlePromptsList(req JSONRPCRequest, encoder *json.Encoder) er
320308 resp := JSONRPCResponse {
321309 JSONRPC : "2.0" ,
322310 ID : req .ID ,
323- Result : map [string ]any {
324- "prompts" : promptList ,
325- },
311+ Result : promptsListResult {Prompts : promptList },
326312 }
327313 return encoder .Encode (resp )
328314}
@@ -354,9 +340,7 @@ func (s *Server) handlePromptsGet(req JSONRPCRequest, encoder *json.Encoder) err
354340 resp := JSONRPCResponse {
355341 JSONRPC : "2.0" ,
356342 ID : req .ID ,
357- Result : map [string ]any {
358- "messages" : messages ,
359- },
343+ Result : promptsGetResult {Messages : messages },
360344 }
361345 return encoder .Encode (resp )
362346}
0 commit comments