mcpField
M
mcp

mcp

github.com/kimny1143開発71 回インストール
リポジトリ

mcp について

Claude Code向けのMCPサーバー開発スキルです。カスタムツールの作成、設定、デバッグに加え、テスト実行やデータベース照会の例を扱います。

主な機能

  1. 01MCPサーバーとカスタムツールの作成
  2. 02プロジェクト単位・ユーザー単位の設定
  3. 03接続状態とサーバーログのデバッグ
  4. 04未使用サーバーの無効化によるコンテキスト節約

活用シーン

  1. 01プロジェクト固有のツール提供
  2. 02外部サービス連携やタスク自動化
  3. 03テスト実行・データベース照会ツールの作成

MCP 対応

MCP SDKを使ったサーバー作成と、Claude Codeでの設定方法を説明します。サーバーはMCPクライアントにカスタムツールを公開します。

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/kimny1143/claude-code-template --skill mcp

インストール後、Claude Code で/mcpを入力して読み込まれたか確認します。

詳細

プロジェクトの README より

mcp - MCP Server 作成

Model Context Protocol (MCP) サーバーの作成・管理。


概要

MCP は Claude Code にカスタムツールを追加するためのプロトコル。

用途:

  • プロジェクト固有のツール提供
  • 外部サービス連携
  • 自動化タスク

基本構造

// 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);

設定

プロジェクトレベル (.mcp.json)
{
  "mcpServers": {
    "my-tool": {
      "command": "node",
      "args": ["scripts/mcp/my-tool.js"]
    }
  }
}
ユーザーレベル (~/.claude/settings.json)
{
  "mcpServers": {
    "my-global-tool": {
      "command": "node",
      "args": ["/path/to/tool.js"]
    }
  }
}

実用例

テストランナー
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 クエリ
{
  name: "query_database",
  description: "データベースにクエリを実行",
  inputSchema: {
    type: "object",
    properties: {
      query: { type: "string", description: "SQL クエリ" },
    },
    required: ["query"],
  },
}

コンテキスト節約

使わないMCPサーバーを無効化してコンテキストを節約:

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

デバッグ

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

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

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

ベストプラクティス

  1. エラーハンドリング: 常に try-catch で囲む
  2. タイムアウト: 長時間処理は避ける(30秒以内)
  3. 説明を明確に: description はツール選択の判断材料
  4. inputSchema 必須: パラメータの型を明示
  5. 10個以下: プロジェクトあたりのMCP数を制限

コメント