Skip to content

Commit 57740b6

Browse files
authored
task: add _meta field to relevant types (#429)
1 parent 96de112 commit 57740b6

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

mcp/prompts.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type GetPromptResult struct {
4747
// that requires argument values to be provided when calling prompts/get.
4848
// If Arguments is nil or empty, this is a static prompt that takes no arguments.
4949
type Prompt struct {
50+
// Meta is a metadata object that is reserved by MCP for storing additional information.
51+
Meta *Meta `json:"_meta,omitempty"`
5052
// The name of the prompt or prompt template.
5153
Name string `json:"name"`
5254
// An optional description of what this prompt provides

mcp/tools.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ type ToolListChangedNotification struct {
545545

546546
// Tool represents the definition for a tool the client can call.
547547
type Tool struct {
548+
// Meta is a metadata object that is reserved by MCP for storing additional information.
549+
Meta *Meta `json:"_meta,omitempty"`
548550
// The name of the tool.
549551
Name string `json:"name"`
550552
// A human-readable description of the tool.

mcp/types.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ func (m *Meta) UnmarshalJSON(data []byte) error {
152152
return nil
153153
}
154154

155+
func NewMetaFromMap(m map[string]any) *Meta {
156+
progressToken := m["progressToken"]
157+
if progressToken != nil {
158+
delete(m, "progressToken")
159+
}
160+
161+
return &Meta{
162+
ProgressToken: progressToken,
163+
AdditionalFields: m,
164+
}
165+
}
166+
155167
type Request struct {
156168
Method string `json:"method"`
157169
Params RequestParams `json:"params,omitempty"`
@@ -233,7 +245,7 @@ func (p *NotificationParams) UnmarshalJSON(data []byte) error {
233245
type Result struct {
234246
// This result property is reserved by the protocol to allow clients and
235247
// servers to attach additional metadata to their responses.
236-
Meta map[string]any `json:"_meta,omitempty"`
248+
Meta *Meta `json:"_meta,omitempty"`
237249
}
238250

239251
// RequestId is a uniquely identifying ID for a request in JSON-RPC.
@@ -644,6 +656,8 @@ type ResourceUpdatedNotificationParams struct {
644656
// Resource represents a known resource that the server is capable of reading.
645657
type Resource struct {
646658
Annotated
659+
// Meta is a metadata object that is reserved by MCP for storing additional information.
660+
Meta *Meta `json:"_meta,omitempty"`
647661
// The URI of this resource.
648662
URI string `json:"uri"`
649663
// A human-readable name for this resource.
@@ -668,6 +682,8 @@ func (r Resource) GetName() string {
668682
// on the server.
669683
type ResourceTemplate struct {
670684
Annotated
685+
// Meta is a metadata object that is reserved by MCP for storing additional information.
686+
Meta *Meta `json:"_meta,omitempty"`
671687
// A URI template (according to RFC 6570) that can be used to construct
672688
// resource URIs.
673689
URITemplate *URITemplate `json:"uriTemplate"`
@@ -697,6 +713,8 @@ type ResourceContents interface {
697713
}
698714

699715
type TextResourceContents struct {
716+
// Meta is a metadata object that is reserved by MCP for storing additional information.
717+
Meta *Meta `json:"_meta,omitempty"`
700718
// The URI of this resource.
701719
URI string `json:"uri"`
702720
// The MIME type of this resource, if known.
@@ -709,6 +727,8 @@ type TextResourceContents struct {
709727
func (TextResourceContents) isResourceContents() {}
710728

711729
type BlobResourceContents struct {
730+
// Meta is a metadata object that is reserved by MCP for storing additional information.
731+
Meta *Meta `json:"_meta,omitempty"`
712732
// The URI of this resource.
713733
URI string `json:"uri"`
714734
// The MIME type of this resource, if known.
@@ -867,6 +887,8 @@ type Content interface {
867887
// It must have Type set to "text".
868888
type TextContent struct {
869889
Annotated
890+
// Meta is a metadata object that is reserved by MCP for storing additional information.
891+
Meta *Meta `json:"_meta,omitempty"`
870892
Type string `json:"type"` // Must be "text"
871893
// The text content of the message.
872894
Text string `json:"text"`
@@ -878,6 +900,8 @@ func (TextContent) isContent() {}
878900
// It must have Type set to "image".
879901
type ImageContent struct {
880902
Annotated
903+
// Meta is a metadata object that is reserved by MCP for storing additional information.
904+
Meta *Meta `json:"_meta,omitempty"`
881905
Type string `json:"type"` // Must be "image"
882906
// The base64-encoded image data.
883907
Data string `json:"data"`
@@ -891,6 +915,8 @@ func (ImageContent) isContent() {}
891915
// It must have Type set to "audio".
892916
type AudioContent struct {
893917
Annotated
918+
// Meta is a metadata object that is reserved by MCP for storing additional information.
919+
Meta *Meta `json:"_meta,omitempty"`
894920
Type string `json:"type"` // Must be "audio"
895921
// The base64-encoded audio data.
896922
Data string `json:"data"`
@@ -922,6 +948,8 @@ func (ResourceLink) isContent() {}
922948
// benefit of the LLM and/or the user.
923949
type EmbeddedResource struct {
924950
Annotated
951+
// Meta is a metadata object that is reserved by MCP for storing additional information.
952+
Meta *Meta `json:"_meta,omitempty"`
925953
Type string `json:"type"`
926954
Resource ResourceContents `json:"resource"`
927955
}
@@ -1056,6 +1084,8 @@ type ListRootsResult struct {
10561084

10571085
// Root represents a root directory or file that the server can operate on.
10581086
type Root struct {
1087+
// Meta is a metadata object that is reserved by MCP for storing additional information.
1088+
Meta *Meta `json:"_meta,omitempty"`
10591089
// The URI identifying the root. This *must* start with file:// for now.
10601090
// This restriction may be relaxed in future versions of the protocol to allow
10611091
// other URI schemes.

mcp/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func ParseGetPromptResult(rawMessage *json.RawMessage) (*GetPromptResult, error)
567567
meta, ok := jsonContent["_meta"]
568568
if ok {
569569
if metaMap, ok := meta.(map[string]any); ok {
570-
result.Meta = metaMap
570+
result.Meta = NewMetaFromMap(metaMap)
571571
}
572572
}
573573

@@ -633,7 +633,7 @@ func ParseCallToolResult(rawMessage *json.RawMessage) (*CallToolResult, error) {
633633
meta, ok := jsonContent["_meta"]
634634
if ok {
635635
if metaMap, ok := meta.(map[string]any); ok {
636-
result.Meta = metaMap
636+
result.Meta = NewMetaFromMap(metaMap)
637637
}
638638
}
639639

@@ -715,7 +715,7 @@ func ParseReadResourceResult(rawMessage *json.RawMessage) (*ReadResourceResult,
715715
meta, ok := jsonContent["_meta"]
716716
if ok {
717717
if metaMap, ok := meta.(map[string]any); ok {
718-
result.Meta = metaMap
718+
result.Meta = NewMetaFromMap(metaMap)
719719
}
720720
}
721721

server/sse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ func TestSSEServer(t *testing.T) {
12571257
WithHooks(&Hooks{
12581258
OnAfterInitialize: []OnAfterInitializeFunc{
12591259
func(ctx context.Context, id any, message *mcp.InitializeRequest, result *mcp.InitializeResult) {
1260-
result.Meta = map[string]any{"invalid": func() {}} // marshal will fail
1260+
result.Meta = mcp.NewMetaFromMap(map[string]any{"invalid": func() {}}) // marshal will fail
12611261
},
12621262
},
12631263
}),

0 commit comments

Comments
 (0)