モデルコンテキストプロトコル (MCP)
AIエージェントのためのツールとアクションの定義を支援する、カスタマイズされたSquid MCP Serverを作成します。
MCPとは?
MCP (Model Context Protocol) は、AIエージェントと外部サービス間の通信を促進するためのオープンプロトコルです。Squid AIエージェントは、MCPサーバーに接続することでツールにアクセスし、定義されたツールを通じて様々な機能を実行できます。
SquidのMCP機能
Squidは、MCPサーバーの作成と管理のための組み込みサポートを提供しており、AIエージェントが利用できるツールとアクションを簡単に定義できます。
The @mcpServer Decorator
MCP Serverを作成するには、バックエンドプロジェクト内のSquidServiceを拡張したサービスに、単に@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デコレーターを使用してサーバーにツールを追加します。各ツールは独自の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サーバーを通じて公開され、ツールの説明に基づいて接続されたAIエージェントにより呼び出されます。
@mcpAuthorizerでMCPを保護する
認証されたAIエージェントのみがMCPサーバーにアクセスできるようにするため、@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サーバーを使用するには、MCPコネクタを追加し、それをAIエージェントのアビリティにリンクする必要があります。
- MCPコネクタの詳細については、documentationを参照してください。
- アビリティの詳細については、documentationを参照してください。