1.0.0 • Published 4 months ago
mcp-inspector v1.0.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, transports, 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
.
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.
1.0.0
4 months ago