1.12.0 • Published 9 months ago
mcp-client v1.12.0
MCP Client
An MCP client for Node.js.
Why?
- 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
listToolsandreadResource, but those names look a bit awkward in TypeScript. This client uses more typical method names, likegetToolsandgetResource.
Usage
Creating a client
import { MCPClient } from "mcp-client";
const client = new MCPClient({
name: "Test",
version: "1.0.0",
});Connecting using stdio
await client.connect({
type: "stdio",
args: ["--port", "8080"],
command: "node",
env: {
PORT: "8080",
},
});Connecting using SSE
await client.connect({
type: "sse",
url: "http://localhost:8080/sse",
});Pinging the server
await client.ping();Calling a tool
const result = await client.callTool({
name: "add",
arguments: { a: 1, b: 2 },
});Calling a tool with a custom result schema
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(),
}),
),
}),
},
);Listing tools
const tools = await client.getAllTools();Listing resources
const resources = await client.getAllResources();Reading a resource
const resource = await client.getResource({ uri: "file:///logs/app.log" });Getting a prompt
const prompt = await client.getPrompt({ name: "git-commit" });Listing prompts
const prompts = await client.getAllPrompts();Setting the logging level
await client.setLoggingLevel("debug");Completing a prompt
const result = await client.complete({
ref: { type: "ref/prompt", name: "git-commit" },
argument: { name: "changes", value: "Add a new feature" },
});Listing resource templates
const resourceTemplates = await client.getAllResourceTemplates();Receiving logging messages
client.on("loggingMessage", (message) => {
console.log(message);
});!NOTE Equivalent to
setNotificationHandler(LoggingMessageNotificationSchema, (message) => { ... })in the MCP TypeScript SDK.