The MCP TypeScript SDK is the official TypeScript client for the Model Context Protocol. Use it from Node.js when your agent, or the service around it, is written in TypeScript.
The example connects to https://api.nexus402.dev/v1/mcp over streamable HTTP, sends your key in the X-Nexus-Api-Key header, lists the tools the key may call, and calls the one you name in NEXUS402_TOOL. A call to a paid tool is charged to your wallet. If you name none, nothing is called. It is run against a test gateway at @modelcontextprotocol/sdk 1.32.2, so a release that changes how headers are passed is caught before you meet it.
Install and run #
TypeScript. Pass the header in the transport's requestInit.
npm install
export NEXUS402_API_KEY=nx_dev_...
# The tool to call, by its name in the marketplace. A paid tool is charged.
export NEXUS402_TOOL='<tool name>'
node main.ts
The example #
// Call a Nexus402 tool with the official MCP TypeScript SDK (ADR-0236).
//
// The key comes from NEXUS402_API_KEY and travels in the X-Nexus-Api-Key
// header; it is never written in code. NEXUS402_TOOL names the tool to call
// (required: a call is charged) and NEXUS402_ARGS its JSON arguments.
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
const url = process.env.NEXUS402_MCP_URL ?? "https://api.nexus402.dev/v1/mcp";
const key = process.env.NEXUS402_API_KEY;
if (!key) throw new Error("Set NEXUS402_API_KEY");
const name = process.env.NEXUS402_TOOL;
// Never a default: a call is charged, and you choose who is paid.
if (!name) {
throw new Error("Set NEXUS402_TOOL to the name of the tool to call. Nothing was called. A call to a paid tool is charged to your wallet.");
}
const client = new Client({ name: "nexus402-example", version: "1.0.0" });
await client.connect(new StreamableHTTPClientTransport(new URL(url), { requestInit: { headers: { "X-Nexus-Api-Key": key } } }));
const { tools } = await client.listTools();
console.log(`${tools.length} tools listed`);
const args = JSON.parse(process.env.NEXUS402_ARGS ?? "{}") as Record<string, unknown>;
let code = 0;
try {
const result = await client.callTool({ name, arguments: args });
if (result.isError) {
console.log(`call refused: ${JSON.stringify(result.content)}`);
code = 2;
} else {
for (const block of result.content as { type: string; text?: string }[]) console.log(block.text ?? JSON.stringify(block));
}
} catch (error) {
// A 402 or another refusal.
console.log(`call refused: ${String(error)}`);
code = 2;
}
await client.close();
// exitCode, not process.exit(): exiting while the transport's handles close
// aborts Node on Windows.
process.exitCode = code;
When a call is refused #
A call your wallet or your key's cap cannot cover is refused with HTTP 402 before anything is charged, and the example prints call refused and exits with code 2. A call that fails before any output reaches your agent is not charged either. Every refusal code is in the error reference.
Next #
- All four framework examples, side by side.
- Keys, caps and rotation, for a key per agent.
- The marketplace, for the tools your agent can call.