0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MCP(Model Context Protocol)入門 — 基礎から自作MCPサーバーまで実践ガイド

Posted at

MCP(Model Context Protocol)入門 — 基礎から自作MCPサーバーまで実践ガイド

こんにちは。最近話題になっている Model Context Protocol (MCP) について、基礎から触ってみて、さらに簡単なMCPサーバーを自作してみたので、その内容をまとめます。
実際に触ってみると「なるほど、こうやってLLMと外部ツールをつなぐのか」という理解が深まったので、これから触る方の参考になればと思います。


1. MCPとは?

MCP(Model Context Protocol)は、LLM(大規模言語モデル)と外部のツールやデータソースを接続するための新しいプロトコルです。
これまでLLMと外部サービスをつなぐには独自の実装が必要でしたが、MCPを利用すると「標準的な方法」でLLMが外部ツールを呼び出せるようになります。

イメージ的には、以下のような仕組みです:

  • LLM(ChatGPTなど)がMCPクライアント
  • 外部のサービスやデータベースがMCPサーバー
  • 両者の間をMCPが仲介して、共通のルールで通信する

2. 環境構築

今回はNode.jsでMCPサーバーを動かす例を試しました。
事前に以下を準備しておきます:

  • Node.js(v18以上推奨)
  • npm / yarn
  • 任意のエディタ(VSCodeなど)

3. サンプルMCPサーバーを作る

公式のSDKを使って、簡単な「Hello World」MCPサーバーを作ってみます。

まずはパッケージをインストールします:

npm install @modelcontextprotocol/sdk

次に、簡単なMCPサーバーを実装します。

import { Server } from "@modelcontextprotocol/sdk";

// サーバーを作成
const server = new Server({
  name: "hello-mcp-server",
  version: "1.0.0",
});

// 「hello」というツールを登録
server.tool("hello", async (args) => {
  const name = args?.name || "world";
  return { message: `Hello, ${name}!` };
});

// サーバー起動
server.listen();

このサーバーを起動すると、MCPクライアントから「hello」ツールを呼び出せるようになります。


4. 実際に試してみる

実際にMCP対応のクライアント(例: Claude Desktop)からこのサーバーを読み込ませると、以下のように利用できます。

> tool:hello { "name": "Qiita" }
{ "message": "Hello, Qiita!" }

とてもシンプルですが、ここから先は外部APIを叩いたり、DBにアクセスしたりと応用範囲が広がります。


5. 自作MCPをClaude Desktopに組み込む方法

実際に作ったMCPサーバーを Claude Desktop に組み込む方法も紹介します。

  1. Claude Desktopの設定ファイルを編集します。
    設定ファイルは以下の場所にあります:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • Windows: %APPDATA%/Claude/claude_desktop_config.json
  2. mcpServers セクションに、自作サーバーを追記します。例:

{
  "mcpServers": {
    "hello-mcp-server": {
      "command": "node",
      "args": ["path/to/hello-mcp-server.js"]
    }
  }
}
  1. Claude Desktopを再起動すると、自作のMCPサーバーが有効になります。

これで、自分が作ったMCPサーバーをClaude上で直接呼び出せるようになります!


まとめ

  • MCPはLLMと外部サービスをつなぐための新しい標準プロトコル
  • SDKを使えば簡単にMCPサーバーを作れる
  • Claude Desktopに組み込めば、手元で作ったツールをそのまま使える

自分で動かしてみると理解が進むので、ぜひ試してみてください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?