Transports
Architecture
How the server is served over stdio or Streamable HTTP from one server factory, how each transport picks the MCP protocol era, and how the HTTP app routes, checks Host and Origin, and shuts down.
The server speaks MCP over stdio (the default) or over Streamable HTTP (--http or MCP_TRANSPORT=http). Both transports build their servers from the same factory, createServer() in src/server.ts, so every client sees identical tools, resources and instructions whatever the transport or protocol version.12
The server supports two MCP protocol eras: 2025-11-25 and 2026-07-28. The SDK picks the era per stdio connection or per HTTP request: a 2026-07-28 client negotiates with server/discover, and a 2025-era client opens with initialize.13 The tests run real MCP clients on both eras, in process and over HTTP.45
This page is for contributors. To run the HTTP transport, see HTTP transport; for every environment variable, see Configuration.
Packages
| Package | Version range | Used for |
|---|---|---|
@modelcontextprotocol/server |
^2.4.0 |
McpServer, serveStdio (from @modelcontextprotocol/server/stdio), createMcpHandler |
@modelcontextprotocol/node |
^2.1.2 |
toNodeHandler, hostHeaderValidation, originValidation |
@modelcontextprotocol/client (dev) |
^2.4.0 |
Clients in the tests |
Versions are from package.json.678
Entry point
src/index.ts defines main(). It chooses HTTP when process.argv includes --http or MCP_TRANSPORT is exactly http, and stdio otherwise. It logs Starting NSF Awards MCP server with the version and the chosen transport.7
main() runs only when the module is executed directly. The entry resolves process.argv[1] with realpathSync and compares it to import.meta.url, so the check also passes when the server is started through an npm bin symlink, and importing the module does not start a server.7
Process handling differs by transport:7
| Event | stdio | HTTP |
|---|---|---|
SIGINT, SIGTERM |
process.exit(0) |
Logs Shutting down, calls server.close() and exits 0 when it finishes; a 5000 ms timer (unref) forces exit 0 if connections stay open |
uncaughtException |
Logs Uncaught exception, exits 1 |
Same |
unhandledRejection |
Logs Unhandled rejection, exits 1 |
Same |
stdio
In stdio mode main() calls serveStdio(() => createServer(), { onerror }). The onerror callback logs Server error with the message.7
serveStdio owns era selection for the connection. A 2026-07-28 client negotiates through server/discover. A 2025-era client that opens with initialize is pinned to an instance from the same factory. Both are served by identical tool definitions.7
stdout carries the protocol, so nothing in src/ may write to it. The logger sends every level to stderr; see Request flow.1
Streamable HTTP
src/http.ts exports three functions:8
| Function | What it does |
|---|---|
httpOptionsFromEnv(env = process.env) |
Builds HttpOptions from HOST, PORT, MCP_ALLOWED_HOSTS and MCP_ALLOWED_ORIGINS |
createHttpApp(options) |
Builds a node:http server that is not yet listening (the tests listen on an ephemeral port) |
startHttpServer(options = httpOptionsFromEnv()) |
Calls createHttpApp, listens on options.port and options.host, and logs NSF Awards MCP server listening with the URL and the Host allow list (or disabled) |
createHttpApp wraps the server factory with createMcpHandler(() => createServer(), { onerror }) from @modelcontextprotocol/server, and adapts that handler to Node with toNodeHandler from @modelcontextprotocol/node. Errors from each layer are logged as MCP HTTP error and MCP HTTP adapter error.8 The handler serves both protocol eras.85
Options
HttpOptions has four fields. httpOptionsFromEnv fills them as follows:85
| Field | Source | Default and rules |
|---|---|---|
host |
HOST |
127.0.0.1 when unset or empty |
port |
PORT |
3000; any value that is not a positive integer (abc, 0, -1, 80.5, empty) also gives 3000 |
allowedHosts |
MCP_ALLOWED_HOSTS, comma-separated |
When the list is empty: localhost, 127.0.0.1, [::1] if host is loopback, otherwise [], which disables the Host check |
allowedOrigins |
MCP_ALLOWED_ORIGINS, comma-separated |
When the list is empty: localhost, 127.0.0.1, [::1] |
List values are split on commas, trimmed, and blank entries dropped, so a variable that holds only commas and spaces counts as empty. host counts as loopback when it is localhost, 127.0.0.1, [::1] or ::1.85
Binding to loopback (the default) therefore enables DNS-rebinding protection for localhost. Binding elsewhere, for example HOST=0.0.0.0 in a container, without MCP_ALLOWED_HOSTS disables the Host check; the comment in http.ts expects the server to sit behind a proxy that sets the public host. Browser Origins stay limited to localhost unless MCP_ALLOWED_ORIGINS is set.85
Routing
The request handler routes on the URL pathname only, so a query string is ignored:85
| Path | Method | Response |
|---|---|---|
/health |
GET |
200, Content-Type: application/json, body {"status":"ok","version":"<package version>"} |
/health |
any other | 405 with the same JSON body |
/mcp |
any | Host check, then Origin check, then the MCP handler |
anything else, including /mcpx |
any | 404, body {"error":"Not found; the MCP endpoint is /mcp"} |
The version in /health is VERSION from src/version.ts, read from package.json.9 /health is not subject to the Host or Origin checks.8
Host and Origin checks
For /mcp, the Host header is checked first with hostHeaderValidation(allowedHosts) (skipped when allowedHosts is empty), then the Origin header with originValidation(allowedOrigins). Either check writes the rejection itself; the handler then returns without calling MCP.8 The tests show the shape of a rejection: HTTP 403 with a JSON-RPC error body whose error.code is -32000. A request with an allowed Host and no Origin header is accepted.5
One factory, many servers
serveStdio and createMcpHandler each receive the factory () => createServer(), not a server instance, and build servers from it.781 createServer() itself keeps nothing between calls, so the instances share only module-level singletons: the nsfApiClient (and its search cache, see NSF API client) and the logger.2
Testing the transports
tests/server/server.test.ts drives createServer() in process on both eras: the legacy era through InMemoryTransport and server.connect, the modern era through serveStdio(factory, { transport }) with a client pinned to 2026-07-28.1 tests/server/http.test.ts starts createHttpApp() on an ephemeral port and connects StreamableHTTPClientTransport clients on both eras; it uses raw node:http requests for the Host check, because fetch will not send a custom Host header.5 See Testing.