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サーバー × LibreChat × Ollama(qwen2.5)

Last updated at Posted at 2025-07-15

無料、かつ、ローカルで完結するMCPサーバー環境構築方法をまとめます!

環境

  • MCPサーバー(Node.js)
  • Docker
  • LibreChat(MCPクライアント)
  • Ollama(qwen2.5)

LLMモデルはMCP toolsを使用できるモデルであれば、自由に選んで大丈夫です!

自作MCPサーバー構築

新規TypeScriptプロジェクト作成

npm init -y
npm install --save-dev typescript
npx tsc --init

MCPサーバー作成

src/index.tsにMCPサーバーの処理を作成します。
以下の例では、Notionページ作成用のMCPツールを作成してます。
他のツールを作成したい場合は、server.tool(---)のブロックを追加で作成すれば、ひとつのMCPサーバーで複数ツールを保持することが可能です。

index.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import fetch from "node-fetch";
import { JSDOM } from "jsdom";
import { Readability } from "@mozilla/readability";
import html2md from "html-to-md";
import { Client as NotionClient } from "@notionhq/client";


const server = new McpServer({
    name: "My MCP Server",
    version: "1.0.0"
});

// Notionクライアントの初期化
const notion = new NotionClient({ auth: process.env.NOTION_API_TOKEN });

// Notionページ作成ツール
server.tool(
    "notion",
    "指定したデータベースに新しいNotionページを作成します",
    {
        databaseId: z.string().describe("ページを追加するNotionデータベースのID"),
        title: z.string().describe("ページタイトル"),
        content: z.string().describe("ページ本文(Markdown可)"),
    },
    async ({ databaseId, title, content }) => {
        // ページ作成
        const response = await notion.pages.create({
            parent: { database_id: databaseId },
            properties: {
                title: [
                    {
                        type: "text",
                        text: { content: title }
                    }
                ]
            },
            children: [
                {
                    object: "block",
                    type: "paragraph",
                    paragraph: {
                        rich_text: [
                            {
                                type: "text",
                                text: { content }
                            }
                        ]
                    }
                }
            ]
        });

        return {
            content: [
                {
                    type: "text",
                    text: `Notionページを作成しました: https://www.notion.so/${response.id.replace(/-/g, "")}`
                }
            ]
        };
    }
);

async function main() {
    const transport = new StdioServerTransport();
    await server.connect(transport);
    console.error("MCP Server running on stdio");
}

main().catch((error) => {
    console.error("Fatal error in main():", error);
    process.exit(1);
});

処理を書き終えたらビルドします。

npx tsc

LibreChat構築

LibreChatはMCPクライアントとして使用できるOSSです(他にも機能が色々ありそうです)。
LLMモデルを画面上で選択できたり、AIエージェント作成などの機能もあり、便利だと思います!

LibreChat資産取得

git clone https://github.com/danny-avila/LibreChat.git

docker-composeファイル編集

作成したMCPサーバー資産をDocker内で参照できるよう、マウント設定などを追記します。

docker-compose.override.yml

services:
  api:
    volumes:
      - type: bind
        source: [MCPサーバーを作成したプロジェクトフォルダパス]
        target: /my-mcp
      - type: bind
        source: ./librechat.yaml
        target: /app/librechat.yaml
      - npm_global: /usr/local/lib/node_modules
volumes:
  npm_global:

設定ファイル編集

設定ファイルに、MCPサーバー情報を追記します。
"/my-mcp/build/index.js"の部分は作成したMCPサーバーのindex.jsファイルがある場所に適宜変更してください。(my-mcpの部分はocker-compose.override.ymlファイルでマウント設定したところを記載してください)

librechat.yaml

mcpServers:
  default:
    command: node
    args:
      - "/my-mcp/build/index.js"
    env:
      NOTION_API_TOKEN: "xxxxxx"

これらの設定をすることで、Docker起動時にMCPクライアントとしてMCPツール読込などを自動でしてくれます!

Ollamaローカル起動

LLMの環境準備については、ここでは省略しますm(__)m
LibreChat画面でAPIキー設定ができたり、様々なLLMモデルを使用できるので色々試してみてください!

Docker起動

docker-compose up -d --build

LibreChatでの操作例

  • http://localhost:3080でLibreChat画面表示
  • エージェント作成
  • (推奨)システムプロンプト(指示文)で、ツール名を明記
  • LLMモデル選択
  • ツール選択(ここで自作MCPサーバーのツールを選択できる)
  • エージェント選択
  • チャット
  • LLMが判断してMCPサーバーのツールを使用

まとめ

以上、無料、かつ、ローカルで完結するMCPサーバー環境構築方法をご紹介しました!

この構成により、完全無料かつローカルで完結するMCPツール開発・運用環境が整います。LibreChatと組み合わせることで、独自のAIエージェントに任意のツールを追加し、実行させることが可能です。

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?