🤖

[TypeScript] fastmcp を使ってコンパクトに MCP Server を作ろう

2025/03/27に公開

TL;DR

  • fastmcp が、TypeScript での MCP Server の実装に便利
  • 少ないコード量でサクッと実装できる

fastmcp とは

Model Context Protocol (MCP) のサーバーを構築するための TypeScript フレームワーク。少ないコード量で MCP サーバーを実装できます。

https://github.com/punkpeye/fastmcp

実装例

下記は文字列の長さを計算するだけどサンプルの MCP サーバーです。Deno を使って書いてます。

import { FastMCP } from "npm:fastmcp@1.20.5";
import { z } from "npm:zod@3.24.2";

const server = new FastMCP({
    name: "Sample FastMCP Server",
    version: "1.0.0"
});

server.addTool({
    name: "Count String Length",
    description: "Count the length of a string",
    parameters: z.object({
        string: z.string()
    }),
    execute: (params) => {
        const length = params.string.length;
        return Promise.resolve(JSON.stringify({
            length
        }));
    }
});

server.start({
    transportType: "stdio"
});

このくらい少ないコード量で MCP サーバーが実装できます。

mcp.json の設定

ちなみに、MCP クライアント側には、下記のような設定を追加します。

{
  "mcpServers": {
    "github.com/heavenosk/fastmcp": {
      "command": "deno",
      "args": [
        "run",
        "--allow-all",
        // コードの絶対パスを指定します
        "/path/server.ts"
      ]
    }
  }
}

まとめ

めちゃくちゃシンプルに実装できるので、とりあえず使ってみるのがいいと思います!

ちなみに、下記の記事で Vitepress の記事を MCP Server 化する際も fastmcp を使っていますので、参考にしてみてください。

https://zenn.dev/heavenosk/articles/vitepress-mcp

Discussion