Skip to content

Commit fc4fcf3

Browse files
Update tool api
1 parent f331ff1 commit fc4fcf3

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131

3232
s := mcp.NewServer(mcp.NewStdioServerTransport())
3333
err := s.Tool("hello", "Say hello to a person", func(arguments MyFunctionsArguments) (*tools.ToolResponse, error) {
34-
return tools.NewToolTextResponse(fmt.Sprintf("Hello, %s!", arguments.Submitter)), nil
34+
return tools.NewToolTextResponseContent(fmt.Sprintf("Hello, %s!", arguments.Submitter)), nil
3535
})
3636
if err != nil {
3737
panic(err)

tools/internal/tool_json.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package internal

tools/tool_api.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,32 @@ const (
8282
ContentTypeText ContentType = "text"
8383
ContentTypeImage ContentType = "image"
8484
ContentTypeEmbeddedResource ContentType = "embedded-resource"
85-
ContentTypeError ContentType = "error"
8685
)
8786

8887
// This is a union type of all the different ToolResponse that can be sent back to the client.
8988
// We allow creation through constructors only to make sure that the ToolResponse is valid.
9089
type ToolResponse struct {
90+
Content []ToolResponseContent
91+
Error error
92+
}
93+
94+
func NewToolReponse(content ...ToolResponseContent) *ToolResponse {
95+
return &ToolResponse{
96+
Content: content,
97+
}
98+
}
99+
100+
type ToolResponseContent struct {
91101
Type ContentType
92102
TextContent *TextContent
93103
ImageContent *ImageContent
94104
EmbeddedResource *EmbeddedResource
95105
Annotations *ContentAnnotations
96-
Error error
97106
}
98107

99-
func (c *ToolResponse) WithAnnotations(annotations ContentAnnotations) *ToolResponse {
108+
// Custom JSON marshaling for ToolResponse.
109+
110+
func (c *ToolResponseContent) WithAnnotations(annotations ContentAnnotations) *ToolResponseContent {
100111
c.Annotations = &annotations
101112
return c
102113
}
@@ -105,34 +116,33 @@ func (c *ToolResponse) WithAnnotations(annotations ContentAnnotations) *ToolResp
105116
// This is used to create a result that will be returned to the client as an error for a tool call.
106117
func NewToolError(err error) *ToolResponse {
107118
return &ToolResponse{
108-
Type: ContentTypeError,
109119
Error: err,
110120
}
111121
}
112122

113-
// NewToolImageResponse creates a new ToolResponse that is an image.
123+
// NewToolImageResponseContent creates a new ToolResponse that is an image.
114124
// The given data is base64-encoded
115-
func NewToolImageResponse(base64EncodedStringData string, mimeType string) *ToolResponse {
116-
return &ToolResponse{
125+
func NewToolImageResponseContent(base64EncodedStringData string, mimeType string) *ToolResponseContent {
126+
return &ToolResponseContent{
117127
Type: ContentTypeImage,
118128
ImageContent: &ImageContent{Data: base64EncodedStringData, MimeType: mimeType},
119129
}
120130
}
121131

122-
// NewToolTextResponse creates a new ToolResponse that is a simple text string.
132+
// NewToolTextResponseContent creates a new ToolResponse that is a simple text string.
123133
// The client will render this as a single string.
124-
func NewToolTextResponse(content string) *ToolResponse {
125-
return &ToolResponse{
134+
func NewToolTextResponseContent(content string) *ToolResponseContent {
135+
return &ToolResponseContent{
126136
Type: ContentTypeText,
127137
TextContent: &TextContent{Text: content},
128138
}
129139
}
130140

131-
// NewToolBlobResourceResponse creates a new ToolResponse that is a blob of binary data.
141+
// NewToolBlobResourceResponseContent creates a new ToolResponse that is a blob of binary data.
132142
// The given data is base64-encoded; the client will decode it.
133143
// The client will render this as a blob; it will not be human-readable.
134-
func NewToolBlobResourceResponse(uri string, base64EncodedData string, mimeType string) *ToolResponse {
135-
return &ToolResponse{
144+
func NewToolBlobResourceResponseContent(uri string, base64EncodedData string, mimeType string) *ToolResponseContent {
145+
return &ToolResponseContent{
136146
Type: ContentTypeEmbeddedResource,
137147
EmbeddedResource: &EmbeddedResource{
138148
EmbeddedResourceType: EmbeddedResourceTypeBlob,
@@ -144,11 +154,11 @@ func NewToolBlobResourceResponse(uri string, base64EncodedData string, mimeType
144154
}
145155
}
146156

147-
// NewToolTextResourceResponse creates a new ToolResponse that is an embedded resource of type "text".
157+
// NewToolTextResourceResponseContent creates a new ToolResponse that is an embedded resource of type "text".
148158
// The given text is embedded in the response as a TextResourceContents, which
149159
// contains the given MIME type and URI. The text is not base64-encoded.
150-
func NewToolTextResourceResponse(uri string, text string, mimeType string) *ToolResponse {
151-
return &ToolResponse{
160+
func NewToolTextResourceResponseContent(uri string, text string, mimeType string) *ToolResponseContent {
161+
return &ToolResponseContent{
152162
Type: ContentTypeEmbeddedResource,
153163
EmbeddedResource: &EmbeddedResource{
154164
EmbeddedResourceType: EmbeddedResourceTypeText,

0 commit comments

Comments
 (0)