Skip to content

Commit 7e6fe09

Browse files
authored
Merge pull request #26 from xinwo/feature/strong_content_type
change to strong content type
2 parents e8d90a0 + d783f8f commit 7e6fe09

File tree

9 files changed

+224
-54
lines changed

9 files changed

+224
-54
lines changed

client/sse.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,7 @@ func (c *SSEMCPClient) GetPrompt(
462462
return nil, err
463463
}
464464

465-
var result mcp.GetPromptResult
466-
if err := json.Unmarshal(*response, &result); err != nil {
467-
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
468-
}
469-
470-
return &result, nil
465+
return mcp.ParseGetPromptResult(response)
471466
}
472467

473468
func (c *SSEMCPClient) ListTools(
@@ -496,12 +491,7 @@ func (c *SSEMCPClient) CallTool(
496491
return nil, err
497492
}
498493

499-
var result mcp.CallToolResult
500-
if err := json.Unmarshal(*response, &result); err != nil {
501-
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
502-
}
503-
504-
return &result, nil
494+
return mcp.ParseCallToolResult(response)
505495
}
506496

507497
func (c *SSEMCPClient) SetLevel(

client/sse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestSSEMCPClient(t *testing.T) {
2525
mcp.WithString("parameter-1", mcp.Description("A string tool parameter")),
2626
), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
2727
return &mcp.CallToolResult{
28-
Content: []interface{}{
28+
Content: []mcp.Content{
2929
mcp.TextContent{
3030
Type: "text",
3131
Text: "Input parameter: " + request.Params.Arguments["parameter-1"].(string),

client/stdio.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,7 @@ func (c *StdioMCPClient) GetPrompt(
388388
return nil, err
389389
}
390390

391-
var result mcp.GetPromptResult
392-
if err := json.Unmarshal(*response, &result); err != nil {
393-
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
394-
}
395-
396-
return &result, nil
391+
return mcp.ParseGetPromptResult(response)
397392
}
398393

399394
func (c *StdioMCPClient) ListTools(
@@ -422,12 +417,7 @@ func (c *StdioMCPClient) CallTool(
422417
return nil, err
423418
}
424419

425-
var result mcp.CallToolResult
426-
if err := json.Unmarshal(*response, &result); err != nil {
427-
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
428-
}
429-
430-
return &result, nil
420+
return mcp.ParseCallToolResult(response)
431421
}
432422

433423
func (c *StdioMCPClient) SetLevel(

examples/everything/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func (s *MCPServer) handleEchoTool(
280280
return nil, fmt.Errorf("invalid message argument")
281281
}
282282
return &mcp.CallToolResult{
283-
Content: []interface{}{
283+
Content: []mcp.Content{
284284
mcp.TextContent{
285285
Type: "text",
286286
Text: fmt.Sprintf("Echo: %s", message),
@@ -301,7 +301,7 @@ func (s *MCPServer) handleAddTool(
301301
}
302302
sum := a + b
303303
return &mcp.CallToolResult{
304-
Content: []interface{}{
304+
Content: []mcp.Content{
305305
mcp.TextContent{
306306
Type: "text",
307307
Text: fmt.Sprintf("The sum of %f and %f is %f.", a, b, sum),
@@ -330,7 +330,7 @@ func (s *MCPServer) handleSendNotification(
330330
}
331331

332332
return &mcp.CallToolResult{
333-
Content: []interface{}{
333+
Content: []mcp.Content{
334334
mcp.TextContent{
335335
Type: "text",
336336
Text: "notification sent successfully",
@@ -373,7 +373,7 @@ func (s *MCPServer) handleLongRunningOperationTool(
373373
}
374374

375375
return &mcp.CallToolResult{
376-
Content: []interface{}{
376+
Content: []mcp.Content{
377377
mcp.TextContent{
378378
Type: "text",
379379
Text: fmt.Sprintf(
@@ -412,7 +412,7 @@ func (s *MCPServer) handleGetTinyImageTool(
412412
request mcp.CallToolRequest,
413413
) (*mcp.CallToolResult, error) {
414414
return &mcp.CallToolResult{
415-
Content: []interface{}{
415+
Content: []mcp.Content{
416416
mcp.TextContent{
417417
Type: "text",
418418
Text: "This is a tiny image:",

mcp/prompts.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,8 @@ const (
7777
// This is similar to `SamplingMessage`, but also supports the embedding of
7878
// resources from the MCP server.
7979
type PromptMessage struct {
80-
Role Role `json:"role"`
81-
Content interface{} `json:"content"` // Can be TextContent, ImageContent, or EmbeddedResource
82-
}
83-
84-
// EmbeddedResource represents the contents of a resource, embedded into a prompt or tool call result.
85-
//
86-
// It is up to the client how best to render embedded resources for the
87-
// benefit of the LLM and/or the user.
88-
type EmbeddedResource struct {
89-
Annotated
90-
Type string `json:"type"`
91-
Resource ResourceContents `json:"resource"`
80+
Role Role `json:"role"`
81+
Content Content `json:"content"` // Can be TextContent, ImageContent, or EmbeddedResource
9282
}
9383

9484
// PromptListChangedNotification is an optional notification from the server

mcp/tools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type ListToolsResult struct {
2525
// should be reported as an MCP error response.
2626
type CallToolResult struct {
2727
Result
28-
Content []interface{} `json:"content"` // Can be TextContent, ImageContent, or EmbeddedResource
28+
Content []Content `json:"content"` // Can be TextContent, ImageContent, or EmbeddedResource
2929
// Whether the tool call ended in an error.
3030
//
3131
// If not set, this is assumed to be false (the call was successful).

mcp/types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ type Annotated struct {
585585
} `json:"annotations,omitempty"`
586586
}
587587

588+
type Content interface {
589+
isContent()
590+
}
591+
588592
// TextContent represents text provided to or from an LLM.
589593
// It must have Type set to "text".
590594
type TextContent struct {
@@ -594,6 +598,8 @@ type TextContent struct {
594598
Text string `json:"text"`
595599
}
596600

601+
func (TextContent) isContent() {}
602+
597603
// ImageContent represents an image provided to or from an LLM.
598604
// It must have Type set to "image".
599605
type ImageContent struct {
@@ -605,6 +611,20 @@ type ImageContent struct {
605611
MIMEType string `json:"mimeType"`
606612
}
607613

614+
func (ImageContent) isContent() {}
615+
616+
// EmbeddedResource represents the contents of a resource, embedded into a prompt or tool call result.
617+
//
618+
// It is up to the client how best to render embedded resources for the
619+
// benefit of the LLM and/or the user.
620+
type EmbeddedResource struct {
621+
Annotated
622+
Type string `json:"type"`
623+
Resource ResourceContents `json:"resource"`
624+
}
625+
626+
func (EmbeddedResource) isContent() {}
627+
608628
// ModelPreferences represents the server's preferences for model selection,
609629
// requested of the client during sampling.
610630
//

0 commit comments

Comments
 (0)