the_inference_data

Guides /

Call an MCP server from TypeScript

Version 2 of the official TypeScript SDK ships the client as @modelcontextprotocol/client. This lists the tools on The Inference's server and calls one. It was run against the live server with version 2.4.0 on Node.

npm install @modelcontextprotocol/client
client.mjs
import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client";

const key = process.env.INFERENCE_KEY; // tik_...; six tools answer without one
const transport = new StreamableHTTPClientTransport(new URL("https://theinference.org/api/mcp"), {
  authProvider: key ? { token: async () => key } : undefined,
});

const client = new Client({ name: "my-agent", version: "1.0.0" });
await client.connect(transport);

const { tools } = await client.listTools();
console.log(tools.map((t) => t.name));

const result = await client.callTool({ name: "search", arguments: { query: "Nvidia" } });
console.log(result.content[0].text.slice(0, 300));

await client.close();

What the parts do

  • authProvider sets the Authorization: Bearer header before every request from whatever token() returns. Use it for API keys.
  • connect() runs the MCP handshake. Nothing is sent until you call it.
  • Coming from v1 (@modelcontextprotocol/sdk)? The class names are the same; only the import path moved.

Hand the tools to a model

Each tool from listTools() has a name, a description and a JSON Schema for its input. Give those to your model as tool definitions, run the ones it asks for with callTool, and return the results.