Skip to main content

GitHub Copilot SDK での MCP サーバーの使用

Copilot SDK は MCP サーバー (モデル コンテキスト プロトコル) と統合して、アシスタントの機能を外部ツールで拡張できます。 MCP サーバーは個別のプロセスとして実行され、会話中に呼び出すことができるCopilotツール (関数) を公開します。

メモ

これは進化する機能です。 進行中のディスカッションについては 、問題 #36 を参照してください。

MCP とは

モデル コンテキスト プロトコル (MCP) は、AI アシスタントを外部のツールやデータ ソースに接続するためのオープンな標準です。 MCP サーバーは次のことができます。

  • コードまたはスクリプトを実行する
  • データベースのクエリ
  • ファイル システムにアクセスする
  • 外部 API を呼び出す
  • その他

サーバーの種類

SDK では、次の 2 種類の MCP サーバーがサポートされています。

タイプ説明ユースケース(事例)
Local/Stdioサブプロセスとして実行され、stdin/stdout 経由で通信するローカル ツール、ファイル アクセス、カスタム スクリプト
HTTP/SSEHTTP 経由でアクセスされるリモート サーバー共有サービス、クラウドでホストされるツール

コンフィギュレーション

Node.js/ TypeScript

import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient();
const session = await client.createSession({
    model: "gpt-5",
    mcpServers: {
        // Local MCP server (stdio)
        "my-local-server": {
            type: "local",
            command: "node",
            args: ["./mcp-server.js"],
            env: { DEBUG: "true" },
            cwd: "./servers",
            tools: ["*"],  // "*" = all tools, [] = none, or list specific tools
            timeout: 30000,
        },
        // Remote MCP server (HTTP)
        "github": {
            type: "http",
            url: "https://api.githubcopilot.com/mcp/",
            headers: { "Authorization": "Bearer ${TOKEN}" },
            tools: ["*"],
        },
    },
});

Python

import asyncio
from copilot import CopilotClient
from copilot.session import PermissionHandler

async def main():
    client = CopilotClient()
    await client.start()

    session = await client.create_session(on_permission_request=PermissionHandler.approve_all, model="gpt-5", mcp_servers={
        # Local MCP server (stdio)
        "my-local-server": {
            "type": "local",
            "command": "python",
            "args": ["./mcp_server.py"],
            "env": {"DEBUG": "true"},
            "cwd": "./servers",
            "tools": ["*"],
            "timeout": 30000,
        },
        # Remote MCP server (HTTP)
        "github": {
            "type": "http",
            "url": "https://api.githubcopilot.com/mcp/",
            "headers": {"Authorization": "Bearer ${TOKEN}"},
            "tools": ["*"],
        },
    })

    response = await session.send_and_wait("List my recent GitHub notifications")
    print(response.data.content)

    await client.stop()

asyncio.run(main())

Go

package main

import (
    "context"
    "log"
    copilot "github.com/github/copilot-sdk/go"
)

func main() {
    ctx := context.Background()
    client := copilot.NewClient(nil)
    if err := client.Start(ctx); err != nil {
        log.Fatal(err)
    }
    defer client.Stop()

    session, err := client.CreateSession(ctx, &copilot.SessionConfig{
        Model: "gpt-5",
        MCPServers: map[string]copilot.MCPServerConfig{
            "my-local-server": copilot.MCPStdioServerConfig{
                Command: "node",
                Args:    []string{"./mcp-server.js"},
                Tools:   []string{"*"},
            },
        },
    })
    if err != nil {
        log.Fatal(err)
    }
    defer session.Disconnect()

    // Use the session...
}

.NET

using GitHub.Copilot;

await using var client = new CopilotClient();
await using var session = await client.CreateSessionAsync(new SessionConfig
{
    Model = "gpt-5",
    McpServers = new Dictionary<string, McpServerConfig>
    {
        ["my-local-server"] = new McpStdioServerConfig
        {
            Command = "node",
            Args = new List<string> { "./mcp-server.js" },
            Tools = new List<string> { "*" },
        },
    },
});

ツールの構成

tools フィールドを使用して、MCP サーバーで使用できるツールを制御できます。

すべてのツールを許可する

"*"を使用して、MCP サーバーによって提供されるすべてのツールを有効にします。

tools: ["*"]

特定のツールを許可する

アクセスを制限するツール名の一覧を指定します。

tools: ["bash", "edit"]

エージェントで使用できるのは、一覧に示されているツールのみです。

すべてのツールを無効にする

空の配列を使用して、すべてのツールを無効にします。

tools: []

Notes

  • tools フィールドでは、使用できるツールを定義します。
  • 個別の allow または disallow 構成はありません。ツール アクセスは、この一覧を通じて直接制御されます。

クイック スタート: ファイルシステム MCP サーバー

公式の @modelcontextprotocol/server-filesystem MCP サーバーを使用した完全な作業例を次に示します。

import { CopilotClient } from "@github/copilot-sdk";

async function main() {
    const client = new CopilotClient();

    // Create session with filesystem MCP server
    const session = await client.createSession({
        mcpServers: {
            filesystem: {
                type: "local",
                command: "npx",
                args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
                tools: ["*"],
            },
        },
    });

    console.log("Session created:", session.sessionId);

    // The model can now use filesystem tools
    const result = await session.sendAndWait({
        prompt: "List the files in the allowed directory",
    });

    console.log("Response:", result?.data?.content);

    await session.disconnect();
    await client.stop();
}

main();

Output:

Session created: 18b3482b-bcba-40ba-9f02-ad2ac949a59a
Response: The allowed directory is `/tmp`, which contains various files
and subdirectories including temporary system files, log files, and
directories for different applications.

ヒント

MCP サーバー ディレクトリから任意の MCP サーバーを使用できます。 一般的なオプションには、 @modelcontextprotocol/server-github@modelcontextprotocol/server-sqlite@modelcontextprotocol/server-puppeteerなどがあります。

構成オプション

ローカル/stdio サーバー

財産タイプ必須説明
type
"local" または "stdio"Noサーバーの種類 (既定値はローカル)
commandstringはい実行するコマンド
argsstring[]はいコマンド引数
envobjectNo環境変数
cwdstringNo作業ディレクトリ
toolsstring[]Noすべてを有効にするツール (["*"] すべて有効), ([] なし)
timeoutnumberNoタイムアウト (ミリ秒)

リモート サーバー (HTTP/SSE)

財産タイプ必須説明
type
"http" または "sse"はいサーバーの種類
urlstringはいサーバー アドレス
headersobjectNoHTTP ヘッダー (認証用など)
toolsstring[]No有効にするツール
timeoutnumberNoタイムアウト (ミリ秒)

Troubleshooting

ツールが表示されない、または呼び出されていない

  1. MCP サーバーが正しく起動されていることを確認する

    • コマンドと引数が正しいことを確認する
    • 起動時にサーバー プロセスがクラッシュしないことを確認する
    • stderr でエラー出力を探す
  2. ツールの構成を確認する

    • tools["*"]に設定されているか、必要な特定のツールが一覧表示されていることを確認します
    • 空の配列 [] は、ツールが有効になっていないこと
  3. リモート サーバーの接続を確認する

    • URL にアクセスできることを確認する
    • 認証ヘッダーが正しいことを確認する

一般的な問題

Issueソリューション
"MCP サーバーが見つかりません"コマンド パスが正しく、実行可能であることを確認する
"接続が拒否されました" (HTTP)URL を確認し、サーバーが実行されていることを確認します
"タイムアウトエラー"
timeout値を増やすか、サーバーのパフォーマンスを確認する
ツールは機能しますが、呼び出されませんプロンプトにツールの機能が明確に必要であることを確認する

デバッグの詳細なガイダンスについては、 MCP サーバー デバッグ ガイド を参照してください。

こちらも参照ください