MCP TypeScript SDK (V2) / @modelcontextprotocol/node / toNodeHandler
toNodeHandler
Interfaces
FetchLikeMcpHandler
Defined in: packages/middleware/node/src/toNodeHandler.ts:60
The web-standard fetch face of an McpHttpHandler (or any fetch-shaped MCP handler) — the only surface toNodeHandler touches. Accepting the face structurally keeps the adapter usable with hand-wired compositions that route over isLegacyRequest and produce a Response directly.
Properties
fetch
fetch: (
request,options?) =>Promise<Response>
Defined in: packages/middleware/node/src/toNodeHandler.ts:61
Parameters
request
Request
options?
Returns
Promise<Response>
NodeIncomingMessageLike
Defined in: packages/middleware/node/src/toNodeHandler.ts:36
Minimal duck-typed shape of a Node.js IncomingMessage accepted by toNodeHandler. Kept structural so the adapter stays free of node: imports.
Extends
AsyncIterable<unknown>
Properties
auth?
optionalauth?:AuthInfo
Defined in: packages/middleware/node/src/toNodeHandler.ts:41
Validated authentication info attached by upstream middleware (pass-through).
headers
headers:
Record<string,string|string[] |undefined>
Defined in: packages/middleware/node/src/toNodeHandler.ts:39
method?
optionalmethod?:string
Defined in: packages/middleware/node/src/toNodeHandler.ts:37
url?
optionalurl?:string
Defined in: packages/middleware/node/src/toNodeHandler.ts:38
Methods
[asyncIterator]()
[asyncIterator]():
AsyncIterator<unknown,any,any>
Defined in: node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts:38
Returns
AsyncIterator<unknown, any, any>
Inherited from
AsyncIterable.[asyncIterator]
NodeServerResponseLike
Defined in: packages/middleware/node/src/toNodeHandler.ts:45
Minimal duck-typed shape of a Node.js ServerResponse accepted by toNodeHandler.
Properties
destroyed?
optionaldestroyed?:boolean
Defined in: packages/middleware/node/src/toNodeHandler.ts:50
Methods
end()
end(
chunk?):unknown
Defined in: packages/middleware/node/src/toNodeHandler.ts:48
Parameters
chunk?
string | Uint8Array<ArrayBufferLike>
Returns
unknown
on()
on(
event,listener):unknown
Defined in: packages/middleware/node/src/toNodeHandler.ts:49
Parameters
event
string
listener
(...args) => void
Returns
unknown
write()
write(
chunk):unknown
Defined in: packages/middleware/node/src/toNodeHandler.ts:47
Parameters
chunk
string | Uint8Array<ArrayBufferLike>
Returns
unknown
writeHead()
writeHead(
statusCode,headers?):unknown
Defined in: packages/middleware/node/src/toNodeHandler.ts:46
Parameters
statusCode
number
headers?
Record<string, string>
Returns
unknown
ToNodeHandlerOptions
Defined in: packages/middleware/node/src/toNodeHandler.ts:73
Options for toNodeHandler.
Properties
onerror?
optionalonerror?: (error) =>void
Defined in: packages/middleware/node/src/toNodeHandler.ts:81
Called when the adapter answers 500 because request conversion or handler.fetch itself threw (e.g. a closed handler). Restores the observability the removed .node face had via the entry's own onerror — entry-internal failures are still reported through handler.fetch and surface via the entry's onerror option as before.
Parameters
error
Error
Returns
void
ToWebRequestOptions
Defined in: packages/middleware/node/src/toNodeHandler.ts:190
Options for toWebRequest.
Properties
signal?
optionalsignal?:AbortSignal
Defined in: packages/middleware/node/src/toNodeHandler.ts:192
An AbortSignal to attach to the constructed Request (request.signal).
Type Aliases
NodeMcpRequestHandler
NodeMcpRequestHandler = (
req,res,parsedBody?) =>Promise<void>
Defined in: packages/middleware/node/src/toNodeHandler.ts:70
A Node.js (req, res, parsedBody?) request handler produced by toNodeHandler. The third argument is an optional pre-parsed body (req.body from express.json()); a function third argument (Express's next when the handler is mounted as middleware) is ignored.
Parameters
req
res
parsedBody?
unknown
Returns
Promise<void>
Functions
toNodeHandler()
toNodeHandler(
handler,opts?):NodeMcpRequestHandler
Defined in: packages/middleware/node/src/toNodeHandler.ts:97
Adapts a web-standard MCP handler (handler.fetch) to a Node.js (req, res, parsedBody?) request handler. The returned function converts the Node request to a web-standard Request, calls handler.fetch, then writes the Response back to res (honoring write backpressure for streamed SSE responses).
req.auth is forwarded as the handler's pass-through authInfo. A function third argument (Express's next) is ignored, never treated as a body.
Pass { onerror } to observe the adapter-level error fallback (request conversion / handler.fetch throw) before the 500 response is written.
Parameters
handler
opts?
Returns
toWebRequest()
toWebRequest(
req,parsedBody?,options?):Promise<Request>
Defined in: packages/middleware/node/src/toNodeHandler.ts:211
Convert a Node.js IncomingMessage (duck-typed — an Express req works) to the web-standard Request that handler.fetch() and isLegacyRequest() take. This is the conversion toNodeHandler performs internally, exported for hand-wired compositions:
const probe = await toWebRequest(req, req.body);
await ((await isLegacyRequest(probe)) ? legacy(req, res) : modern(req, res, req.body));With no parsedBody the Node stream is read to completion — read the body from the returned Request afterwards, not from req. When a body parser already consumed the stream (express.json()), pass the parsed value as parsedBody and nothing is read from req.
Parameters
req
parsedBody?
unknown
options?
Returns
Promise<Request>