MCP ExplorerExplorer

Mcp Helper

@mizchion 9 months ago
4 MIT
FreeCommunity
AI Systems
Type-safe library for creating MCP servers in Deno with Zod validation.

Overview

What is Mcp Helper

mcp-helper is a type-safe helper library designed for creating Model Context Protocol (MCP) servers in Deno, utilizing TypeScript for enhanced safety and reliability.

Use cases

Use cases for mcp-helper include developing APIs that require strict type checking, creating tools for data processing, and building interactive applications that need robust server-side logic.

How to use

To use mcp-helper, import the library and create an MCP server by defining tools with Zod schemas for input and output validation. Connect the server using a transport layer, such as StdioServerTransport.

Key features

Key features include type-safe MCP server creation, Zod schema validation for input/output, an in-memory test client for easy testing, and built-in TypeScript type inference for tools.

Where to use

mcp-helper can be used in various fields that require reliable server-client communication, particularly in applications that leverage TypeScript and Deno for backend development.

Content

@mizchi/mcp-helper

A type-safe helper library for creating Model Context Protocol (MCP) servers in Deno.

Features

  • Type-safe MCP server creation with TypeScript
  • Zod schema validation for input/output
  • In-memory test client for easy testing
  • Built-in TypeScript type inference for tools

Installation

import { createToolsServer } from "jsr:@mizchi/mcp-helper";

Usage

Creating an MCP Server

import { createToolsServer } from "jsr:@mizchi/mcp-helper";
import { StdioServerTransport } from "npm:@modelcontextprotocol/[email protected]/server/stdio.js";
import { z } from "zod";

// Define your tools with Zod schemas
const tools = [
  {
    name: "getStringLength",
    description: "Get length of input string",
    inputSchema: z.object({
      input: z.string().describe("The input string"),
    }),
    outputSchema: z.number(),
  },
] as const;

// Create the server with type-safe handlers
const server = createToolsServer(
  {
    name: "my-server",
    version: "1.0.0",
  },
  tools,
  // define handlers for all tools
  {
    getStringLength(params: { input: string }) {
      return params.input.length;
    },
  }
);
await server.connect(new StdioServerTransport());

Testing Your Server

The library provides an in-memory test client for easy testing:

import { createInMemoryTestClient } from "@mizchi/mcp-helper";

// Create a test client
const client = await createInMemoryTestClient<typeof tools>(server);

// Call tools with type safety
const result = await client.callTool("getStringLength", {
  input: "Hello, world!",
});

console.log(result); // 13

// Clean up
await client.close();

MCP Configuration

Add your server to the MCP configuration:

{
  "mcpServers": {
    "mymcp": {
      "command": "deno",
      "args": [
        "run",
        "-A",
        "server.ts"
      ],
      "env": {},
      "disabled": false,
      "alwaysAllow": []
    }
  }
}

API

createToolsServer<T extends Tools>(info, tools, handlers)

Creates a new MCP server with type-safe tool definitions.

  • info: Server information (name and version)
  • tools: Array of tool definitions with Zod schemas
  • handlers: Implementation of tool handlers

createInMemoryTestClient<T extends Tools>(server)

Creates a test client for the given server.

  • server: The MCP server instance
  • Returns: A type-safe client for testing tools

CLI (Experimental)

CAUTION: You can not install cli via jsr for dynamic import restrictions.

# Install cli
$ deno install -Afg ./lmcp.ts -A
$ lmcp examples/readUrl.ts readUrl -- --url=https://zenn.dev/mizchi/articles/deno-mcp-server

Add this export for your server impl.

// add this export for impl
export default server;

Development

# Run tests
deno task test

License

MIT

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers