0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonで簡易的なmcpサーバー

0
Posted at

1.はじめに

名前は聞いたことあるけど結局何なのってなったときのための超簡単な説明記事を残しておこうと思い、記事にしました。

2. MCPって?

MCP(Model Context Protocol)の略で、LLMを利用するアプリと外部ツールを接続するプロトコルのことらしいです。イメージとしては

AIアプリケーション
        ↓
       MCP
        ↓
  MCPサーバー
        ↓
外部API / DB / ファイルなど

3.簡易的なサーバーを作る

環境

Python 3.12.10
uv
MCP Python SDK 2.1.1
Node.js 22.19.0
MCP Inspector 2.3.0

実践

uvでプロジェクトを作成後、mcp sdkを追加します。

$ uv add "mcp[cli]"

プロジェクトフォルダ直下に以下のファイルを追加します。

server.py
from mcp.server import MCPServer

#サーバーの作成
mcp = MCPServer("Sample Server")

# toolとして公開
@mcp.tool()
def add(a: int, b: int) -> int:
    """2つの数字を足し算します。"""
    return a + b

下記、MCP Inspectorを動かします。

$ uv run mcp dev server.py

起動後、URLが表示されるため、そちらで作業を行います。
サーバーに接続し、「Tools」からaddを選択し、足したい数字を入れると結果出ます。

image.png

4.解説

解説と言っても単純にmcp = MCPServer("Sample Server")でサーバを作成し、@mcp.tool()でtoolを公開しているだけです。
今回簡単な足し算にしていますが、この公開するtoolをデータベース検索や、API呼び出しにすることで様々な処理を「Tool」というもので提供できるようです。

5.まとめ

何より今回は環境の設定にめちゃくちゃ苦戦しました、、、(MCP Inspectorのバージョンに踊らされた)

各種処理をtoolとして公開できるのは便利なものだと思いました。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?