diff --git a/mcp/consts.go b/mcp/consts.go new file mode 100644 index 000000000..66eb3803b --- /dev/null +++ b/mcp/consts.go @@ -0,0 +1,9 @@ +package mcp + +const ( + ContentTypeText = "text" + ContentTypeImage = "image" + ContentTypeAudio = "audio" + ContentTypeLink = "resource_link" + ContentTypeResource = "resource" +) diff --git a/mcp/types.go b/mcp/types.go index 909ebd892..588f912ef 100644 --- a/mcp/types.go +++ b/mcp/types.go @@ -8,8 +8,9 @@ import ( "maps" "strconv" - "github.com/yosida95/uritemplate/v3" "net/http" + + "github.com/yosida95/uritemplate/v3" ) type MCPMethod string @@ -1113,23 +1114,23 @@ func UnmarshalContent(data []byte) (Content, error) { } switch contentType { - case "text": + case ContentTypeText: var content TextContent err := json.Unmarshal(data, &content) return content, err - case "image": + case ContentTypeImage: var content ImageContent err := json.Unmarshal(data, &content) return content, err - case "audio": + case ContentTypeAudio: var content AudioContent err := json.Unmarshal(data, &content) return content, err - case "resource_link": + case ContentTypeLink: var content ResourceLink err := json.Unmarshal(data, &content) return content, err - case "resource": + case ContentTypeResource: var content EmbeddedResource err := json.Unmarshal(data, &content) return content, err diff --git a/mcp/utils.go b/mcp/utils.go index 3e652efd7..b63c73d82 100644 --- a/mcp/utils.go +++ b/mcp/utils.go @@ -198,7 +198,7 @@ func NewPromptMessage(role Role, content Content) PromptMessage { // Helper function to create a new TextContent func NewTextContent(text string) TextContent { return TextContent{ - Type: "text", + Type: ContentTypeText, Text: text, } } @@ -207,7 +207,7 @@ func NewTextContent(text string) TextContent { // Helper function to create a new ImageContent func NewImageContent(data, mimeType string) ImageContent { return ImageContent{ - Type: "image", + Type: ContentTypeImage, Data: data, MIMEType: mimeType, } @@ -216,7 +216,7 @@ func NewImageContent(data, mimeType string) ImageContent { // Helper function to create a new AudioContent func NewAudioContent(data, mimeType string) AudioContent { return AudioContent{ - Type: "audio", + Type: ContentTypeAudio, Data: data, MIMEType: mimeType, } @@ -225,7 +225,7 @@ func NewAudioContent(data, mimeType string) AudioContent { // Helper function to create a new ResourceLink func NewResourceLink(uri, name, description, mimeType string) ResourceLink { return ResourceLink{ - Type: "resource_link", + Type: ContentTypeLink, URI: uri, Name: name, Description: description, @@ -236,7 +236,7 @@ func NewResourceLink(uri, name, description, mimeType string) ResourceLink { // Helper function to create a new EmbeddedResource func NewEmbeddedResource(resource ResourceContents) EmbeddedResource { return EmbeddedResource{ - Type: "resource", + Type: ContentTypeResource, Resource: resource, } } @@ -246,7 +246,7 @@ func NewToolResultText(text string) *CallToolResult { return &CallToolResult{ Content: []Content{ TextContent{ - Type: "text", + Type: ContentTypeText, Text: text, }, }, @@ -258,11 +258,11 @@ func NewToolResultImage(text, imageData, mimeType string) *CallToolResult { return &CallToolResult{ Content: []Content{ TextContent{ - Type: "text", + Type: ContentTypeText, Text: text, }, ImageContent{ - Type: "image", + Type: ContentTypeImage, Data: imageData, MIMEType: mimeType, }, @@ -275,11 +275,11 @@ func NewToolResultAudio(text, imageData, mimeType string) *CallToolResult { return &CallToolResult{ Content: []Content{ TextContent{ - Type: "text", + Type: ContentTypeText, Text: text, }, AudioContent{ - Type: "audio", + Type: ContentTypeAudio, Data: imageData, MIMEType: mimeType, }, @@ -295,11 +295,11 @@ func NewToolResultResource( return &CallToolResult{ Content: []Content{ TextContent{ - Type: "text", + Type: ContentTypeText, Text: text, }, EmbeddedResource{ - Type: "resource", + Type: ContentTypeResource, Resource: resource, }, }, @@ -312,7 +312,7 @@ func NewToolResultError(text string) *CallToolResult { return &CallToolResult{ Content: []Content{ TextContent{ - Type: "text", + Type: ContentTypeText, Text: text, }, }, @@ -330,7 +330,7 @@ func NewToolResultErrorFromErr(text string, err error) *CallToolResult { return &CallToolResult{ Content: []Content{ TextContent{ - Type: "text", + Type: ContentTypeText, Text: text, }, }, @@ -345,7 +345,7 @@ func NewToolResultErrorf(format string, a ...any) *CallToolResult { return &CallToolResult{ Content: []Content{ TextContent{ - Type: "text", + Type: ContentTypeText, Text: fmt.Sprintf(format, a...), }, }, @@ -467,11 +467,11 @@ func ParseContent(contentMap map[string]any) (Content, error) { contentType := ExtractString(contentMap, "type") switch contentType { - case "text": + case ContentTypeText: text := ExtractString(contentMap, "text") return NewTextContent(text), nil - case "image": + case ContentTypeImage: data := ExtractString(contentMap, "data") mimeType := ExtractString(contentMap, "mimeType") if data == "" || mimeType == "" { @@ -479,7 +479,7 @@ func ParseContent(contentMap map[string]any) (Content, error) { } return NewImageContent(data, mimeType), nil - case "audio": + case ContentTypeAudio: data := ExtractString(contentMap, "data") mimeType := ExtractString(contentMap, "mimeType") if data == "" || mimeType == "" { @@ -487,7 +487,7 @@ func ParseContent(contentMap map[string]any) (Content, error) { } return NewAudioContent(data, mimeType), nil - case "resource_link": + case ContentTypeLink: uri := ExtractString(contentMap, "uri") name := ExtractString(contentMap, "name") description := ExtractString(contentMap, "description") @@ -497,7 +497,7 @@ func ParseContent(contentMap map[string]any) (Content, error) { } return NewResourceLink(uri, name, description, mimeType), nil - case "resource": + case ContentTypeResource: resourceMap := ExtractMap(contentMap, "resource") if resourceMap == nil { return nil, fmt.Errorf("resource is missing")