runtime について
runtimeは、サーバー側の@copilotkit/runtimeを設定する開発者向けの索引スキルです。エンドポイントの設置、Agent Runnerの選択、ミドルウェア、組み込み・外部エージェントの接続など、目的に合うリファレンスへ案内します。
主な機能
- 01タスク別にruntime資料を案内
- 02fetch対応エンドポイントの設定ガイド
- 03Agent Runnerの選択肢を解説
- 04ミドルウェアとリクエストフックを記載
- 05組み込み・外部エージェントの接続を説明
活用シーン
- 01CopilotKit runtimeハンドラーの設置
- 02永続化方式とRunner動作の選択
- 03認証やリクエストガードの追加
- 04外部エージェントフレームワークの接続
MCP 対応
runtimeは任意の`mcpApps`設定を通じてMCP Appsミドルウェアを利用できます。入力にはMCPサーバーインターフェースの説明はありません。
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ copilotkit/ copilotkit --skill runtimeインストール後、Claude Code で/runtimeを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
references/agent-runners-custom.mdagent-runners-in-memory.mdagent-runners-sqlite.mdagent-runners.mdbuilt-in-agent-factory-modes.mdbuilt-in-agent-helper-utilities.mdbuilt-in-agent-model-identifiers.mdbuilt-in-agent.mdintelligence-mode.mdmiddleware.mdserver-side-tools.mdsetup-endpoint.mdtranscription.mdwiring-a2a.mdwiring-adk.mdwiring-ag2.mdwiring-agno.mdwiring-aws-strands.mdwiring-crewai-crews.mdwiring-crewai-flows.mdwiring-external-agents.mdwiring-langgraph.mdwiring-llamaindex.mdwiring-mastra.mdwiring-mcp-apps-middleware.mdwiring-ms-agent-framework.mdwiring-pydantic-ai.md詳細
CopilotKit Runtime
@copilotkit/runtime は CopilotKit のサーバー側です。AG-UI プロトコルの
リクエストを受け付け、AbstractAgent(組み込みまたは外部)に振り分け、
AgentRunner を通じてストリームを実行し、Server-Sent Events として応答します。
この SKILL.md は索引です。タスクに合った references/ 内のリファレンスを
読んでください。このファイルだけでパッケージ全体を把握しようとしないでください。
メンタルモデル — CopilotRuntime に渡す3つの辞書
new CopilotRuntime({
agents, // Record<string, AbstractAgent> — see wiring-external-agents or built-in-agent
runner, // AgentRunner (optional) — see agent-runners
intelligence, // CopilotKitIntelligence (optional) — see intelligence-mode (auto-wires runner)
mcpApps, // McpAppsConfig (optional) — see wiring-mcp-apps-middleware
a2ui, // A2UIConfig (optional) — see packages/a2ui-renderer skill
hooks, // { onRequest, onBeforeHandler } — see middleware
beforeRequestMiddleware,
afterRequestMiddleware, // legacy — see middleware
transcription, // TranscriptionService (optional) — see transcription
});
次に、これをマウントします。
import { createCopilotRuntimeHandler } from "@copilotkit/runtime/v2";
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
export default { fetch: handler };
どのリファレンスを読むべきか
| タスク | リファレンス |
|---|---|
| あらゆる Fetch ネイティブサーバー(Cloudflare Workers、Bun、Deno、Vercel Edge、Next.js App Router、React Router v7、TanStack Start)へのマウント、または Express/Node からの委譲 | references/setup-endpoint.md |
hooks.onRequest / hooks.onBeforeHandler(推奨)または従来の beforeRequestMiddleware / afterRequestMiddleware を使用した認証/ロギング/レート制限/リクエストスコープのガード | references/middleware.md |
InMemoryAgentRunner、SqliteAgentRunner、カスタムサブクラスの選択 — スレッドロックのセマンティクスと、runner/Intelligence の相互排他を含む | references/agent-runners.md(バックエンド固有の詳細は -in-memory.md、-sqlite.md、-custom.md を参照) |
| CopilotKit Intelligence(セルフホスト不可のマネージドサービス)による永続スレッドとリアルタイム WebSocket の有効化 | references/intelligence-mode.md |
音声の文字起こし — /transcribe エンドポイント用の TranscriptionService サブクラスの実装 | references/transcription.md |
BuiltInAgent のインスタンス化 — Simple Mode(従来型)、または TanStack AI(AG-UI 準拠の推奨デフォルト)、AI SDK、カスタムファクトリーを使用する Factory Mode | references/built-in-agent.md(-factory-modes.md、-helper-utilities.md、-model-identifiers.md も参照) |
BuiltInAgent.config.tools 用の defineTool によるサーバーサイドツールの定義(Simple Mode のみ) | references/server-side-tools.md |
外部エージェントフレームワークを CopilotRuntime({ agents }) に接続 | references/wiring-external-agents.md(インデックス)と、フレームワークごとのリファレンス(wiring-mastra.md、wiring-langgraph.md、wiring-crewai-crews.md、wiring-crewai-flows.md、wiring-pydantic-ai.md、wiring-adk.md、wiring-llamaindex.md、wiring-agno.md、wiring-aws-strands.md、wiring-ms-agent-framework.md、wiring-ag2.md、wiring-a2a.md) |
| MCP Apps の接続(エージェントではなく、ランタイムレベルのミドルウェア) | references/wiring-mcp-apps-middleware.md |
不変条件と注意点(参照前に一度だけ読み込む)
createCopilotRuntimeHandlerが標準のプリミティブです。createCopilotExpressHandlerとcreateCopilotHonoHandlerもありますが、絶対に使用しないでください。代わりに、Express/Hono のルートから fetch プリミティブへ委譲してください。- Intelligence の認証情報はサーバー側にあります。CLI は
CPK_INTELLIGENCE_API_KEYをランタイムの環境変数に書き込みます。クライアント側のキーは使用しません。 - Intelligence モードでは
IntelligenceAgentRunnerが自動的に接続されます。CopilotRuntimeにrunnerとintelligenceの両方を渡すと、インスタンスの構築時に拒否されます。 - Intelligence モードはマネージドの CopilotKit Intelligence サービス(
api.cloud.copilotkit.ai)を対象としており、セルフホストできません。 hooks.onRequestはbeforeRequestMiddlewareより先に実行されます(Responseによるショートサーキットではフックベースのミドルウェアが優先されます)。beforeRequestMiddlewareはhooks.onRequestの後に実行されます(fetch-handler.ts:136-147を参照)。- Intelligence の
identifyUserは、スローされたResponseオブジェクトを転送しません。500 に変換してください。認証拒否の処理は、Responseを転送するhooks.onRequestで行ってください。 agents__unsafe_dev_onlyとselfManagedAgentsは、互いに別名として使える開発専用の機能です。本番環境では使用しないでください。どちらを指定しても、SPA が開発モードであることを示します。
初めて読む人向けの順序
setup-endpoint— プリミティブ。built-in-agentまたはwiring-external-agentsから選択 — エージェント。agent-runners— 本番環境での永続化方法の選択。- 任意:
middleware、intelligence-mode、server-side-tools、transcription。
コメント
Markdown 対応