validateMetadata checks the metadata of every requests
(id jsonrpc.RequestId, params RequestParams, stdio bool)
| 57 | |
| 58 | // validateMetadata checks the metadata of every requests |
| 59 | func 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 |