1.12.0 • Published 11 months ago

mcp-client v1.12.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

MCP Client

An MCP client for Node.js.

!TIP This client has been tested with FastMCP.

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 listTools and readResource, but those names look a bit awkward in TypeScript. This client uses more typical method names, like getTools and getResource.

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.12.0

11 months ago

1.11.0

11 months ago

1.10.0

11 months ago

1.9.0

11 months ago

1.8.0

11 months ago

1.7.0

11 months ago

1.6.0

11 months ago

1.5.0

11 months ago

1.4.0

11 months ago

1.3.0

11 months ago

1.2.0

11 months ago

1.1.0

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago