SuperAPI AI Directory

mcp

Agents & models

View repository

Updated 10-15 20:04

About this skill

mcp - MCP Server Creation

Creating and managing Model Context Protocol (MCP) servers.


Overview

MCP is a protocol for adding custom tools to Claude Code.

Uses:

  • Providing project-specific tools
  • External service integration
  • Automation tasks

Basic Structure

// scripts/mcp/my-tool.js
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";

const server = new Server(
  { name: "my-tool", version: "1.0.0" },
  { capabilities: { tools: {} } }
);

// ツール一覧
server.setRequestHandler("tools/list", async () => ({
  tools: [
    {
      name: "my_custom_tool",
      description: "カスタムツールの説明",
      inputSchema: {
        type: "object",
        properties: {
          param1: { type: "string", description: "パラメータ1" },
        },
        required: ["param1"],
      },
    },
  ],
}));

// ツール実行
server.setRequestHandler("tools/call", async (request) => {
  const { name, arguments: args } = request.params;

  if (name === "my_custom_tool") {
    const result = await doSomething(args.param1);
    return { content: [{ type: "text", text: JSON.stringify(result) }] };
  }

  throw new Error(`Unknown tool: ${name}`);
});

// サーバー起動
const transport = new StdioServerTransport();
await server.connect(transport);

Configuration

Project level (.mcp.json)

{
  "mcpServers": {
    "my-tool": {
      "command": "node",
      "args": ["scripts/mcp/my-tool.js"]
    }
  }
}

User level (~/.claude/settings.json)

{
  "mcpServers": {
    "my-global-tool": {
      "command": "node",
      "args": ["/path/to/tool.js"]
    }
  }
}

Practical Examples

Test runner

server.setRequestHandler("tools/list", async () => ({
  tools: [
    {
      name: "run_tests",
      description: "ユニットテストを実行",
      inputSchema: {
        type: "object",
        properties: {
          pattern: { type: "string", description: "テストパターン" },
        },
      },
    },
  ],
}));

server.setRequestHandler("tools/call", async (request) => {
  if (request.params.name === "run_tests") {
    const pattern = request.params.arguments?.pattern || "";
    const result = await runTests(pattern);
    return { content: [{ type: "text", text: result }] };
  }
});

async function runTests(pattern) {
  const { execSync } = await import("child_process");
  try {
    const output = execSync(`npm test -- ${pattern}`, { encoding: "utf-8" });
    return output;
  } catch (error) {
    return error.stdout + error.stderr;
  }
}

DB query

{
  name: "query_database",
  description: "データベースにクエリを実行",
  inputSchema: {
    type: "object",
    properties: {
      query: { type: "string", description: "SQL クエリ" },
    },
    required: ["query"],
  },
}

Saving Context

Save context by disabling unused MCP servers:

// .mcp.json
{
  "mcpServers": {
    "frequently-used": { "command": "node", "args": ["tool1.js"] }
  },
  "disabledMcpServers": [
    "rarely-used-tool",
    "heavy-context-tool"
  ]
}

Debugging

# 直接実行
node scripts/mcp/my-tool.js

# ログ確認
DEBUG=mcp* node scripts/mcp/my-tool.js

# Claude Code で確認
# /mcp コマンドで接続状態を確認

Best practices

  1. Error handling: Always wrap in try-catch
  2. Timeout: Avoid long-running processes (within 30 seconds)
  3. Make descriptions clear: description is the basis for tool selection decisions
  4. inputSchema required: Explicitly specify parameter types
  5. 10 or fewer: Limit the number of MCPs per project