Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions client/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,7 @@ func (c *SSEMCPClient) GetPrompt(
return nil, err
}

var result mcp.GetPromptResult
if err := json.Unmarshal(*response, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

return &result, nil
return mcp.ParseGetPromptResult(response)
}

func (c *SSEMCPClient) ListTools(
Expand Down Expand Up @@ -496,12 +491,7 @@ func (c *SSEMCPClient) CallTool(
return nil, err
}

var result mcp.CallToolResult
if err := json.Unmarshal(*response, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

return &result, nil
return mcp.ParseCallToolResult(response)
}

func (c *SSEMCPClient) SetLevel(
Expand Down
2 changes: 1 addition & 1 deletion client/sse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestSSEMCPClient(t *testing.T) {
mcp.WithString("parameter-1", mcp.Description("A string tool parameter")),
), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
return &mcp.CallToolResult{
Content: []interface{}{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: "Input parameter: " + request.Params.Arguments["parameter-1"].(string),
Expand Down
14 changes: 2 additions & 12 deletions client/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,7 @@ func (c *StdioMCPClient) GetPrompt(
return nil, err
}

var result mcp.GetPromptResult
if err := json.Unmarshal(*response, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

return &result, nil
return mcp.ParseGetPromptResult(response)
}

func (c *StdioMCPClient) ListTools(
Expand Down Expand Up @@ -422,12 +417,7 @@ func (c *StdioMCPClient) CallTool(
return nil, err
}

var result mcp.CallToolResult
if err := json.Unmarshal(*response, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

return &result, nil
return mcp.ParseCallToolResult(response)
}

func (c *StdioMCPClient) SetLevel(
Expand Down
10 changes: 5 additions & 5 deletions examples/everything/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (s *MCPServer) handleEchoTool(
return nil, fmt.Errorf("invalid message argument")
}
return &mcp.CallToolResult{
Content: []interface{}{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Echo: %s", message),
Expand All @@ -301,7 +301,7 @@ func (s *MCPServer) handleAddTool(
}
sum := a + b
return &mcp.CallToolResult{
Content: []interface{}{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("The sum of %f and %f is %f.", a, b, sum),
Expand Down Expand Up @@ -330,7 +330,7 @@ func (s *MCPServer) handleSendNotification(
}

return &mcp.CallToolResult{
Content: []interface{}{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: "notification sent successfully",
Expand Down Expand Up @@ -373,7 +373,7 @@ func (s *MCPServer) handleLongRunningOperationTool(
}

return &mcp.CallToolResult{
Content: []interface{}{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf(
Expand Down Expand Up @@ -412,7 +412,7 @@ func (s *MCPServer) handleGetTinyImageTool(
request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
return &mcp.CallToolResult{
Content: []interface{}{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: "This is a tiny image:",
Expand Down
14 changes: 2 additions & 12 deletions mcp/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,8 @@ const (
// This is similar to `SamplingMessage`, but also supports the embedding of
// resources from the MCP server.
type PromptMessage struct {
Role Role `json:"role"`
Content interface{} `json:"content"` // Can be TextContent, ImageContent, or EmbeddedResource
}

// EmbeddedResource represents the contents of a resource, embedded into a prompt or tool call result.
//
// It is up to the client how best to render embedded resources for the
// benefit of the LLM and/or the user.
type EmbeddedResource struct {
Annotated
Type string `json:"type"`
Resource ResourceContents `json:"resource"`
Role Role `json:"role"`
Content Content `json:"content"` // Can be TextContent, ImageContent, or EmbeddedResource
}

// PromptListChangedNotification is an optional notification from the server
Expand Down
2 changes: 1 addition & 1 deletion mcp/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ListToolsResult struct {
// should be reported as an MCP error response.
type CallToolResult struct {
Result
Content []interface{} `json:"content"` // Can be TextContent, ImageContent, or EmbeddedResource
Content []Content `json:"content"` // Can be TextContent, ImageContent, or EmbeddedResource
// Whether the tool call ended in an error.
//
// If not set, this is assumed to be false (the call was successful).
Expand Down
20 changes: 20 additions & 0 deletions mcp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ type Annotated struct {
} `json:"annotations,omitempty"`
}

type Content interface {
isContent()
}

// TextContent represents text provided to or from an LLM.
// It must have Type set to "text".
type TextContent struct {
Expand All @@ -594,6 +598,8 @@ type TextContent struct {
Text string `json:"text"`
}

func (TextContent) isContent() {}

// ImageContent represents an image provided to or from an LLM.
// It must have Type set to "image".
type ImageContent struct {
Expand All @@ -605,6 +611,20 @@ type ImageContent struct {
MIMEType string `json:"mimeType"`
}

func (ImageContent) isContent() {}

// EmbeddedResource represents the contents of a resource, embedded into a prompt or tool call result.
//
// It is up to the client how best to render embedded resources for the
// benefit of the LLM and/or the user.
type EmbeddedResource struct {
Annotated
Type string `json:"type"`
Resource ResourceContents `json:"resource"`
}

func (EmbeddedResource) isContent() {}

// ModelPreferences represents the server's preferences for model selection,
// requested of the client during sampling.
//
Expand Down
Loading