Skip to content

Commit 8a88d01

Browse files
authored
feat:add constants for resource content types (#489)
Signed-off-by: FanOne <294350394@qq.com>
1 parent 9c5d303 commit 8a88d01

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

mcp/consts.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package mcp
2+
3+
const (
4+
ContentTypeText = "text"
5+
ContentTypeImage = "image"
6+
ContentTypeAudio = "audio"
7+
ContentTypeLink = "resource_link"
8+
ContentTypeResource = "resource"
9+
)

mcp/types.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"maps"
99
"strconv"
1010

11-
"github.com/yosida95/uritemplate/v3"
1211
"net/http"
12+
13+
"github.com/yosida95/uritemplate/v3"
1314
)
1415

1516
type MCPMethod string
@@ -1146,23 +1147,23 @@ func UnmarshalContent(data []byte) (Content, error) {
11461147
}
11471148

11481149
switch contentType {
1149-
case "text":
1150+
case ContentTypeText:
11501151
var content TextContent
11511152
err := json.Unmarshal(data, &content)
11521153
return content, err
1153-
case "image":
1154+
case ContentTypeImage:
11541155
var content ImageContent
11551156
err := json.Unmarshal(data, &content)
11561157
return content, err
1157-
case "audio":
1158+
case ContentTypeAudio:
11581159
var content AudioContent
11591160
err := json.Unmarshal(data, &content)
11601161
return content, err
1161-
case "resource_link":
1162+
case ContentTypeLink:
11621163
var content ResourceLink
11631164
err := json.Unmarshal(data, &content)
11641165
return content, err
1165-
case "resource":
1166+
case ContentTypeResource:
11661167
var content EmbeddedResource
11671168
err := json.Unmarshal(data, &content)
11681169
return content, err

mcp/utils.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func NewPromptMessage(role Role, content Content) PromptMessage {
198198
// Helper function to create a new TextContent
199199
func NewTextContent(text string) TextContent {
200200
return TextContent{
201-
Type: "text",
201+
Type: ContentTypeText,
202202
Text: text,
203203
}
204204
}
@@ -207,7 +207,7 @@ func NewTextContent(text string) TextContent {
207207
// Helper function to create a new ImageContent
208208
func NewImageContent(data, mimeType string) ImageContent {
209209
return ImageContent{
210-
Type: "image",
210+
Type: ContentTypeImage,
211211
Data: data,
212212
MIMEType: mimeType,
213213
}
@@ -216,7 +216,7 @@ func NewImageContent(data, mimeType string) ImageContent {
216216
// Helper function to create a new AudioContent
217217
func NewAudioContent(data, mimeType string) AudioContent {
218218
return AudioContent{
219-
Type: "audio",
219+
Type: ContentTypeAudio,
220220
Data: data,
221221
MIMEType: mimeType,
222222
}
@@ -225,7 +225,7 @@ func NewAudioContent(data, mimeType string) AudioContent {
225225
// Helper function to create a new ResourceLink
226226
func NewResourceLink(uri, name, description, mimeType string) ResourceLink {
227227
return ResourceLink{
228-
Type: "resource_link",
228+
Type: ContentTypeLink,
229229
URI: uri,
230230
Name: name,
231231
Description: description,
@@ -236,7 +236,7 @@ func NewResourceLink(uri, name, description, mimeType string) ResourceLink {
236236
// Helper function to create a new EmbeddedResource
237237
func NewEmbeddedResource(resource ResourceContents) EmbeddedResource {
238238
return EmbeddedResource{
239-
Type: "resource",
239+
Type: ContentTypeResource,
240240
Resource: resource,
241241
}
242242
}
@@ -246,7 +246,7 @@ func NewToolResultText(text string) *CallToolResult {
246246
return &CallToolResult{
247247
Content: []Content{
248248
TextContent{
249-
Type: "text",
249+
Type: ContentTypeText,
250250
Text: text,
251251
},
252252
},
@@ -296,11 +296,11 @@ func NewToolResultImage(text, imageData, mimeType string) *CallToolResult {
296296
return &CallToolResult{
297297
Content: []Content{
298298
TextContent{
299-
Type: "text",
299+
Type: ContentTypeText,
300300
Text: text,
301301
},
302302
ImageContent{
303-
Type: "image",
303+
Type: ContentTypeImage,
304304
Data: imageData,
305305
MIMEType: mimeType,
306306
},
@@ -313,11 +313,11 @@ func NewToolResultAudio(text, imageData, mimeType string) *CallToolResult {
313313
return &CallToolResult{
314314
Content: []Content{
315315
TextContent{
316-
Type: "text",
316+
Type: ContentTypeText,
317317
Text: text,
318318
},
319319
AudioContent{
320-
Type: "audio",
320+
Type: ContentTypeAudio,
321321
Data: imageData,
322322
MIMEType: mimeType,
323323
},
@@ -333,11 +333,11 @@ func NewToolResultResource(
333333
return &CallToolResult{
334334
Content: []Content{
335335
TextContent{
336-
Type: "text",
336+
Type: ContentTypeText,
337337
Text: text,
338338
},
339339
EmbeddedResource{
340-
Type: "resource",
340+
Type: ContentTypeResource,
341341
Resource: resource,
342342
},
343343
},
@@ -350,7 +350,7 @@ func NewToolResultError(text string) *CallToolResult {
350350
return &CallToolResult{
351351
Content: []Content{
352352
TextContent{
353-
Type: "text",
353+
Type: ContentTypeText,
354354
Text: text,
355355
},
356356
},
@@ -368,7 +368,7 @@ func NewToolResultErrorFromErr(text string, err error) *CallToolResult {
368368
return &CallToolResult{
369369
Content: []Content{
370370
TextContent{
371-
Type: "text",
371+
Type: ContentTypeText,
372372
Text: text,
373373
},
374374
},
@@ -383,7 +383,7 @@ func NewToolResultErrorf(format string, a ...any) *CallToolResult {
383383
return &CallToolResult{
384384
Content: []Content{
385385
TextContent{
386-
Type: "text",
386+
Type: ContentTypeText,
387387
Text: fmt.Sprintf(format, a...),
388388
},
389389
},
@@ -505,27 +505,27 @@ func ParseContent(contentMap map[string]any) (Content, error) {
505505
contentType := ExtractString(contentMap, "type")
506506

507507
switch contentType {
508-
case "text":
508+
case ContentTypeText:
509509
text := ExtractString(contentMap, "text")
510510
return NewTextContent(text), nil
511511

512-
case "image":
512+
case ContentTypeImage:
513513
data := ExtractString(contentMap, "data")
514514
mimeType := ExtractString(contentMap, "mimeType")
515515
if data == "" || mimeType == "" {
516516
return nil, fmt.Errorf("image data or mimeType is missing")
517517
}
518518
return NewImageContent(data, mimeType), nil
519519

520-
case "audio":
520+
case ContentTypeAudio:
521521
data := ExtractString(contentMap, "data")
522522
mimeType := ExtractString(contentMap, "mimeType")
523523
if data == "" || mimeType == "" {
524524
return nil, fmt.Errorf("audio data or mimeType is missing")
525525
}
526526
return NewAudioContent(data, mimeType), nil
527527

528-
case "resource_link":
528+
case ContentTypeLink:
529529
uri := ExtractString(contentMap, "uri")
530530
name := ExtractString(contentMap, "name")
531531
description := ExtractString(contentMap, "description")
@@ -535,7 +535,7 @@ func ParseContent(contentMap map[string]any) (Content, error) {
535535
}
536536
return NewResourceLink(uri, name, description, mimeType), nil
537537

538-
case "resource":
538+
case ContentTypeResource:
539539
resourceMap := ExtractMap(contentMap, "resource")
540540
if resourceMap == nil {
541541
return nil, fmt.Errorf("resource is missing")

0 commit comments

Comments
 (0)