Model Context Protocol (MCP)
AIエージェント向けのツールとアクションを定義するための、カスタマイズされた Squid MCP Server を作成します。
MCP とは?
MCP(Model Context Protocol)は、AIエージェントと外部サービス間の通信を促進するためのオープンプロトコルです。Squid AI エージェントは、さまざまな機能を定義済みツールとして公開する MCP server に接続することで、ツールへアクセスしアクションを実行できます。
Squid の MCP 機能
Squid は MCP server の作成と管理を組み込みでサポートしており、AIエージェントが利用できるツールとアクションを簡単に定義できます。
@mcpServer デコレーター(Decorator)
MCP Server を作成するには、バックエンドプロジェクトで SquidService
ベースクラスを継承する service に @mcpServer デコレーターを追加するだけです:
import {mcpServer, SquidService} from "@squidcloud/backend";
@mcpServer({
name: 'myMcp',
id: 'myMcp',
description: 'This MCP knows how to provide data about the weather',
version: '1.0.0',
})
export class McpService extends SquidService {
...
}
@mcpTool を使ってツールを追加する
次に、@mcpTool デコレーターを使用して server にツールを追加します。各ツールは独自の input schema とカスタム機能を持つことができます:
import {mcpServer, mcpTool, SquidService} from "@squidcloud/backend";
@mcpServer({
name: 'myMcp',
id: 'myMcp',
description: 'This MCP knows how to provide data about the weather',
version: '1.0.0',
})
export class McpService extends SquidService {
@mcpTool({
description: 'This tool returns the current weather for a given city',
inputSchema: {
type: 'object',
properties: {
city: {
type: 'string',
description: 'The name of the city to get the weather for',
},
},
required: ['city'],
},
})
async getWeather({city} ) {
// Insert custom logic here,
// For example, call a weather API endpoint to get real data
return `The current weather in ${city} is sunny with a temperature of 25°C.`;
}
}
各ツールは MCP server を通じて公開され、接続された AI エージェントによってツールの description に基づいて呼び出されます。
@mcpAuthorizer で MCP をセキュアにする
許可された AI エージェントのみが MCP server にアクセスできるようにするには、@mcpAuthorizer デコレーターを実装します。このメソッドが認可リクエストを処理し、受信したリクエストが有効かどうかを判定します。
import { mcpAuthorizer, McpAuthorizationRequest, mcpTool, mcpServer, SquidService } from '@squidcloud/backend';
@mcpServer({
name: 'myMcp',
id: 'myMcp',
description: 'This MCP knows how to provide data about the weather',
version: '1.0.0',
})
export class McpService extends SquidService {
@mcpAuthorizer()
async authorizeMcp(request: McpAuthorizationRequest): Promise<boolean> {
// Implement authorization logic here
// For example, check if the request has a valid token
const token = request.headers['authorization'];
return token === 'valid-token';
}
}
次のステップ
Squid アプリケーションで MCP server を使用するには、MCP connector を追加し、それを AI エージェントの abilities にリンクする必要があります。
- MCP connector の詳細については、documentation を参照してください。
- abilities の詳細については、documentation を参照してください。