MCPcopy
hub / github.com/googleapis/mcp-toolbox / validateMetadata

Function validateMetadata

internal/server/mcp/vdraft/method.go:59–95  ·  view source on GitHub ↗

validateMetadata checks the metadata of every requests

(id jsonrpc.RequestId, params RequestParams, stdio bool)

Source from the content-addressed store, hash-verified

57
58// validateMetadata checks the metadata of every requests
59func validateMetadata(id jsonrpc.RequestId, params RequestParams, stdio bool) (any, error) {
60 // Check _meta field for protocol version
61 if params.Meta != nil {
62 // check for protocol version metadata
63 v := params.Meta.ProtocolVersion
64 if v == "" {
65 metaErr := fmt.Errorf("missing io.modelcontextprotocol/protocolVersion")
66 return jsonrpc.NewError(id, jsonrpc.INVALID_PARAMS, metaErr.Error(), nil), metaErr
67 }
68 // do not have to verify header for stdio; already verified during stdio
69 // message processing
70 if !stdio {
71 if PROTOCOL_VERSION != v {
72 metaErr := fmt.Errorf("header mismatch: MCP-Protocol-Version header value '%s' does not match body value '%s'", PROTOCOL_VERSION, v)
73 return jsonrpc.NewHeaderMismatchedError(id, metaErr), metaErr
74 }
75 }
76
77 // check for clientInfo
78 clientInfo := params.Meta.ClientInfo
79 if clientInfo.Version == "" || clientInfo.Name == "" {
80 metaErr := fmt.Errorf("missing field from io.modelcontextprotocol/clientInfo")
81 return jsonrpc.NewError(id, jsonrpc.INVALID_PARAMS, metaErr.Error(), nil), metaErr
82 }
83 // check for clientCapabilities
84 clientCapabilities := params.Meta.MetaClientCapabilities
85 if clientCapabilities == nil {
86 metaErr := fmt.Errorf("missing field from io.modelcontextprotocol/clientCapabilities")
87 return jsonrpc.NewError(id, jsonrpc.INVALID_PARAMS, metaErr.Error(), nil), metaErr
88 }
89 // skip checking clientCapabilities since Toolbox do not utilize any of those
90 } else {
91 metaErr := fmt.Errorf("missing required fields in request metadata")
92 return jsonrpc.NewError(id, jsonrpc.INVALID_PARAMS, metaErr.Error(), nil), metaErr
93 }
94 return nil, nil
95}
96
97// validateHeader checks the header of every requests
98// Toolbox do not check for `Mcp-Param-{Name}` header since we are not

Callers 6

serverDiscoverHandlerFunction · 0.85
toolsListHandlerFunction · 0.85
toolsCallHandlerFunction · 0.85
promptsListHandlerFunction · 0.85
promptsGetHandlerFunction · 0.85
TestValidateMetadataFunction · 0.85

Calls 3

NewErrorFunction · 0.92
NewHeaderMismatchedErrorFunction · 0.92
ErrorMethod · 0.65

Tested by 1

TestValidateMetadataFunction · 0.68