An MCP client for Node.js.
[!TIP] This client has been tested with FastMCP.
- MCP TypeScript SDK provides a client for the MCP protocol, but it's a little verbose for my taste. This client abstracts away some of the lower-level details (like pagination, Zod schemas, etc.) and provides a more convenient API.
- The MCP protocol follows some REST-like naming conventions, like
listTools
andreadResource
, but those names look a bit awkward in TypeScript. This client uses more typical method names, likegetTools
andgetResource
.
import { MCPClient } from "mcp-client";
const client = new MCPClient({
name: "Test",
version: "1.0.0",
});
await client.connect({
type: "stdio",
args: ["--port", "8080"],
command: "node",
env: {
PORT: "8080",
},
});
await client.connect({
type: "sse",
url: "http://localhost:8080/sse",
});
await client.ping();
const result = await client.callTool({
name: "add",
arguments: { a: 1, b: 2 },
});
const result = await client.callTool(
{
name: "add",
arguments: { a: 1, b: 2 },
},
{
resultSchema: z.object({
content: z.array(
z.object({
type: z.literal("text"),
text: z.string(),
}),
),
}),
},
);
const tools = await client.getAllTools();
const resources = await client.getAllResources();
const resource = await client.getResource({ uri: "file:///logs/app.log" });
const prompt = await client.getPrompt({ name: "git-commit" });
const prompts = await client.getAllPrompts();
await client.setLoggingLevel("debug");
const result = await client.complete({
ref: { type: "ref/prompt", name: "git-commit" },
argument: { name: "changes", value: "Add a new feature" },
});
const resourceTemplates = await client.getAllResourceTemplates();
client.on("loggingMessage", (message) => {
console.log(message);
});
[!NOTE] Equivalent to
setNotificationHandler(LoggingMessageNotificationSchema, (message) => { ... })
in the MCP TypeScript SDK.