Skip to content

feat: refactor constants for resource content types #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2025
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
9 changes: 9 additions & 0 deletions mcp/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mcp

const (
ContentTypeText = "text"
ContentTypeImage = "image"
ContentTypeAudio = "audio"
ContentTypeLink = "resource_link"
ContentTypeResource = "resource"
)
13 changes: 7 additions & 6 deletions mcp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"maps"
"strconv"

"github.com/yosida95/uritemplate/v3"
"net/http"

"github.com/yosida95/uritemplate/v3"
)

type MCPMethod string
Expand Down Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions mcp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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,
Expand All @@ -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,
}
}
Expand All @@ -246,7 +246,7 @@ func NewToolResultText(text string) *CallToolResult {
return &CallToolResult{
Content: []Content{
TextContent{
Type: "text",
Type: ContentTypeText,
Text: text,
},
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -295,11 +295,11 @@ func NewToolResultResource(
return &CallToolResult{
Content: []Content{
TextContent{
Type: "text",
Type: ContentTypeText,
Text: text,
},
EmbeddedResource{
Type: "resource",
Type: ContentTypeResource,
Resource: resource,
},
},
Expand All @@ -312,7 +312,7 @@ func NewToolResultError(text string) *CallToolResult {
return &CallToolResult{
Content: []Content{
TextContent{
Type: "text",
Type: ContentTypeText,
Text: text,
},
},
Expand All @@ -330,7 +330,7 @@ func NewToolResultErrorFromErr(text string, err error) *CallToolResult {
return &CallToolResult{
Content: []Content{
TextContent{
Type: "text",
Type: ContentTypeText,
Text: text,
},
},
Expand All @@ -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...),
},
},
Expand Down Expand Up @@ -467,27 +467,27 @@ 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 == "" {
return nil, fmt.Errorf("image data or mimeType is missing")
}
return NewImageContent(data, mimeType), nil

case "audio":
case ContentTypeAudio:
data := ExtractString(contentMap, "data")
mimeType := ExtractString(contentMap, "mimeType")
if data == "" || mimeType == "" {
return nil, fmt.Errorf("audio data or mimeType is missing")
}
return NewAudioContent(data, mimeType), nil

case "resource_link":
case ContentTypeLink:
uri := ExtractString(contentMap, "uri")
name := ExtractString(contentMap, "name")
description := ExtractString(contentMap, "description")
Expand All @@ -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")
Expand Down