Exemplo
Test an MCP server in-process over InMemoryTransport, and find out which tool failures arrive as an error result and which as a JSON-RPC error
sha256:d133f23612d4a391e0f2b96f76d38e57a55c2c65d48d5e32fc0099002b56ccfd
CROSS_PASS
L4_CROSS_PASS
MIT-0
Caso
- Objetivo
- Test an MCP server in-process over InMemoryTransport, and find out which tool failures arrive as an error result and which as a JSON-RPC error HOW
- Pacotes
- @modelcontextprotocol/sdk 1.30.0 zod 4.4.3
- Ambiente
- node 22
- Criado
- 2026-08-14T13:24:49Z
Contrato
- assert the SDK version cannot be read by requiring its package.json, and that the transport subpath does not resolve without its .js extension, both because the exports map ends in a wildcard onto the built output
- assert connecting a Client over a linked pair is exactly three messages, initialize out, its response in, and notifications/initialized out carrying no id
- assert 1.30.0 negotiates the protocol version string 2025-11-25, and that no accessor and no own property of the Client or the transport retains it, so the wire is the only place to read it
- assert connect() overwrites the onmessage and onclose already sitting on a transport, which is why recording the wire needs an accessor rather than an assignment
- assert the capabilities the client sees are derived from what was registered, tools and resources each with listChanged and no prompts key at all
- assert the server echoes an older supported version but answers a version it does not know with its own latest instead of an error, and that the SDK's own client refuses such an answer with a plain Error carrying no code
- assert listTools publishes the zod shape as draft-07 JSON Schema with $schema inside it, required omitting the defaulted key, and an execution block the server never asked for
- assert a valid call returns a content array of {type:'text',text} with no isError key at all, and that the handler receives the default already filled in
- assert arguments violating the input schema come back as an isError result rather than a rejected promise, with the JSON-RPC code present only as text, and the handler never running
- assert a missing key, an out-of-range value and omitted arguments all fail that same way, while an undeclared extra property is stripped and the call succeeds
- assert an unknown tool name is also an isError result, while an unknown resource URI and an unregistered method are thrown McpErrors carrying -32602 and -32601
- assert the unknown-resource message carries the 'MCP error -32602' prefix twice, because both the server and the client prepend it
- assert a handler that throws becomes an isError result carrying the bare Error message with no MCP prefix, and that the connection keeps working afterwards
- assert the registered resource is listed with its title and mime type and reads back as the JSON text its handler produced
- assert the linked pair does not serialise, so a Date placed in _meta arrives as a Date and an undefined-valued key survives as an own property
- assert connecting the client first queues initialize on the server half and leaves the promise pending instead of failing fast, then completes the handshake once the server connects
- assert that wait is bounded after all, because initialize is an ordinary request carrying the SDK's default 60 second timeout, and rejects with an McpError of ErrorCode.RequestTimeout that puts the timeout in data
- assert closing either half tears down both, the client's close firing the server's own onclose and leaving it disconnected, and the server's close leaving the client's next call to fail with a plain Error 'Not connected' that carries no code, not an McpError with ConnectionClosed
Arquivos
- csx.json
- package-lock.json
- package.json
- src/server.mjs
- test/contract.mjs
Seeder de origem
Recibos de verificação
- node 22 · CONTAINER_RUN · compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS · node-typescript@1 · 2026-08-14 · ed25519:a2ec939a4c60e243
- · CONTAINER_RUN · compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS · node-typescript@1 · 2026-08-14 · ed25519:7da3f610facce8bf
- · CONTAINER_RUN · compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS · node-typescript@1 · 2026-08-14 · ed25519:7da3f610facce8bf
- · CONTAINER_RUN · compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS · node-typescript@1 · 2026-08-14 · ed25519:7da3f610facce8bf
- · CONTAINER_RUN · compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS · node-typescript@1 · 2026-08-14 · ed25519:7da3f610facce8bf