First running version
This commit is contained in:
48
reference_impl/mcplets/info-pool/api-access/index.ts
Normal file
48
reference_impl/mcplets/info-pool/api-access/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* 外部 API アクセス MCPlet
|
||||
* pool: info-pool | mcpletType: read | visibility: [model]
|
||||
*/
|
||||
import { MCPletServer } from '../../mcplet-server.js';
|
||||
|
||||
const MOCK_SERVICE_URL = process.env.MOCK_SERVICE_URL ?? 'http://localhost:5100';
|
||||
|
||||
const server = new MCPletServer('api-access-mcplet');
|
||||
|
||||
server.registerTool({
|
||||
name: 'call_external_api',
|
||||
description: '外部APIを呼び出してデータを取得します。在庫情報や外部データソースの参照に使用します。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
api: {
|
||||
type: 'string',
|
||||
description: '呼び出すAPI識別子 (例: inventory, customer_stats)',
|
||||
},
|
||||
params: {
|
||||
type: 'object',
|
||||
description: 'APIクエリパラメータ',
|
||||
additionalProperties: true,
|
||||
},
|
||||
},
|
||||
required: ['api'],
|
||||
},
|
||||
mcpletType: 'read',
|
||||
pool: 'info-pool',
|
||||
visibility: ['model'],
|
||||
handler: async (args) => {
|
||||
const api = args['api'] as string;
|
||||
const params = (args['params'] as Record<string, string> | undefined) ?? {};
|
||||
|
||||
if (api === 'inventory') {
|
||||
const item = params['item'] ?? '';
|
||||
const qs = item ? `?item=${encodeURIComponent(item)}` : '';
|
||||
const res = await fetch(`${MOCK_SERVICE_URL}/erp/inventory${qs}`);
|
||||
if (!res.ok) throw new Error(`Inventory API returned ${res.status}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
return { api, params, data: `Mock API response for api="${api}"` };
|
||||
},
|
||||
});
|
||||
|
||||
await server.listen();
|
||||
45
reference_impl/mcplets/info-pool/web-access/index.ts
Normal file
45
reference_impl/mcplets/info-pool/web-access/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 外部 Web アクセス MCPlet
|
||||
* pool: info-pool | mcpletType: read | visibility: [model]
|
||||
*/
|
||||
import { MCPletServer } from '../../mcplet-server.js';
|
||||
|
||||
const MOCK_SERVICE_URL = process.env.MOCK_SERVICE_URL ?? 'http://localhost:5100';
|
||||
|
||||
const server = new MCPletServer('web-access-mcplet');
|
||||
|
||||
server.registerTool({
|
||||
name: 'fetch_web_content',
|
||||
description: '外部Webサイトのコンテンツを取得します。天気予報など外部情報の収集に使用します。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
source: {
|
||||
type: 'string',
|
||||
description: '取得する情報ソース (例: weather_forecast, news)',
|
||||
},
|
||||
date: {
|
||||
type: 'string',
|
||||
description: '対象日付 (YYYY-MM-DD形式)',
|
||||
},
|
||||
},
|
||||
required: ['source'],
|
||||
},
|
||||
mcpletType: 'read',
|
||||
pool: 'info-pool',
|
||||
visibility: ['model'],
|
||||
handler: async (args) => {
|
||||
const source = args['source'] as string;
|
||||
const date = (args['date'] as string | undefined) ?? new Date().toISOString().slice(0, 10);
|
||||
|
||||
if (source === 'weather_forecast' || source === 'weather') {
|
||||
const res = await fetch(`${MOCK_SERVICE_URL}/weather/forecast?date=${date}`);
|
||||
if (!res.ok) throw new Error(`Weather service returned ${res.status}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
return { source, date, content: `Mock web content for source="${source}" on ${date}` };
|
||||
},
|
||||
});
|
||||
|
||||
await server.listen();
|
||||
56
reference_impl/mcplets/internal/crm/index.ts
Normal file
56
reference_impl/mcplets/internal/crm/index.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* CRM MCPlet (内部システム)
|
||||
* pool: なし (pool-less) | mcpletType: read | visibility: [model]
|
||||
*/
|
||||
import { MCPletServer } from '../../mcplet-server.js';
|
||||
|
||||
const MOCK_SERVICE_URL = process.env.MOCK_SERVICE_URL ?? 'http://localhost:5100';
|
||||
|
||||
const server = new MCPletServer('crm-mcplet');
|
||||
|
||||
server.registerTool({
|
||||
name: 'query_crm',
|
||||
description: 'CRMシステムから顧客・予約情報を取得します。キャンセル傾向分析や顧客リスト取得に使用します。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
entity: {
|
||||
type: 'string',
|
||||
enum: ['customers', 'reservations'],
|
||||
description: '取得するエンティティ種別',
|
||||
},
|
||||
filter: {
|
||||
type: 'string',
|
||||
description: 'フィルタ条件 (例: rain_cancel_tendency, date=YYYY-MM-DD)',
|
||||
},
|
||||
},
|
||||
required: ['entity'],
|
||||
},
|
||||
mcpletType: 'read',
|
||||
visibility: ['model'],
|
||||
handler: async (args) => {
|
||||
const entity = args['entity'] as string;
|
||||
const filter = args['filter'] as string | undefined;
|
||||
|
||||
if (entity === 'customers') {
|
||||
const qs = filter ? `?filter=${encodeURIComponent(filter)}` : '';
|
||||
const res = await fetch(`${MOCK_SERVICE_URL}/crm/customers${qs}`);
|
||||
if (!res.ok) throw new Error(`CRM API returned ${res.status}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
if (entity === 'reservations') {
|
||||
// filter may be "date=2026-03-28"
|
||||
const dateMatch = filter?.match(/date=(\d{4}-\d{2}-\d{2})/);
|
||||
const date = dateMatch ? dateMatch[1] : '';
|
||||
const qs = date ? `?date=${date}` : '';
|
||||
const res = await fetch(`${MOCK_SERVICE_URL}/crm/reservations${qs}`);
|
||||
if (!res.ok) throw new Error(`CRM API returned ${res.status}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
throw new Error(`Unknown CRM entity: ${entity}`);
|
||||
},
|
||||
});
|
||||
|
||||
await server.listen();
|
||||
46
reference_impl/mcplets/internal/erp/index.ts
Normal file
46
reference_impl/mcplets/internal/erp/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* ERP MCPlet (内部システム)
|
||||
* pool: なし (pool-less) | mcpletType: read | visibility: [model]
|
||||
*/
|
||||
import { MCPletServer } from '../../mcplet-server.js';
|
||||
|
||||
const MOCK_SERVICE_URL = process.env.MOCK_SERVICE_URL ?? 'http://localhost:5100';
|
||||
|
||||
const server = new MCPletServer('erp-mcplet');
|
||||
|
||||
server.registerTool({
|
||||
name: 'query_erp',
|
||||
description: 'ERPシステムから在庫・発注情報を取得します。商品在庫確認や調達状況の把握に使用します。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
entity: {
|
||||
type: 'string',
|
||||
enum: ['inventory', 'orders'],
|
||||
description: '取得するエンティティ種別',
|
||||
},
|
||||
item: {
|
||||
type: 'string',
|
||||
description: '商品カテゴリ (例: dessert, drink)',
|
||||
},
|
||||
},
|
||||
required: ['entity'],
|
||||
},
|
||||
mcpletType: 'read',
|
||||
visibility: ['model'],
|
||||
handler: async (args) => {
|
||||
const entity = args['entity'] as string;
|
||||
const item = args['item'] as string | undefined;
|
||||
|
||||
if (entity === 'inventory') {
|
||||
const qs = item ? `?item=${encodeURIComponent(item)}` : '';
|
||||
const res = await fetch(`${MOCK_SERVICE_URL}/erp/inventory${qs}`);
|
||||
if (!res.ok) throw new Error(`ERP API returned ${res.status}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
return { entity, message: 'Mock ERP orders data' };
|
||||
},
|
||||
});
|
||||
|
||||
await server.listen();
|
||||
57
reference_impl/mcplets/internal/hr/index.ts
Normal file
57
reference_impl/mcplets/internal/hr/index.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* HR MCPlet (内部システム)
|
||||
* pool: なし (pool-less) | mcpletType: read | visibility: [model]
|
||||
*/
|
||||
import { MCPletServer } from '../../mcplet-server.js';
|
||||
|
||||
const server = new MCPletServer('hr-mcplet');
|
||||
|
||||
server.registerTool({
|
||||
name: 'query_hr',
|
||||
description: 'HRシステムからスタッフ情報・シフトを取得します。施策実行に必要な人員確認に使用します。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
entity: {
|
||||
type: 'string',
|
||||
enum: ['staff', 'shifts'],
|
||||
description: '取得するエンティティ種別',
|
||||
},
|
||||
date: {
|
||||
type: 'string',
|
||||
description: '対象日付 (YYYY-MM-DD形式)',
|
||||
},
|
||||
},
|
||||
required: ['entity'],
|
||||
},
|
||||
mcpletType: 'read',
|
||||
visibility: ['model'],
|
||||
handler: async (args) => {
|
||||
const entity = args['entity'] as string;
|
||||
const date = (args['date'] as string | undefined) ?? new Date().toISOString().slice(0, 10);
|
||||
|
||||
if (entity === 'staff') {
|
||||
return {
|
||||
staff: [
|
||||
{ staffId: 'S001', name: '中村 りか', role: 'manager', available: true },
|
||||
{ staffId: 'S002', name: '小林 けん', role: 'waiter', available: true },
|
||||
{ staffId: 'S003', name: '加藤 みゆ', role: 'waiter', available: false },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
if (entity === 'shifts') {
|
||||
return {
|
||||
date,
|
||||
shifts: [
|
||||
{ staffId: 'S001', startTime: '17:00', endTime: '23:00' },
|
||||
{ staffId: 'S002', startTime: '17:00', endTime: '22:00' },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`Unknown HR entity: ${entity}`);
|
||||
},
|
||||
});
|
||||
|
||||
await server.listen();
|
||||
125
reference_impl/mcplets/mcplet-server.ts
Normal file
125
reference_impl/mcplets/mcplet-server.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* MCPlet Server base — wraps MCP SDK's low-level Server to include
|
||||
* _meta.mcpletType in tools/list responses as required by the MCPlet spec.
|
||||
*/
|
||||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||
import {
|
||||
CallToolRequestSchema,
|
||||
ListToolsRequestSchema,
|
||||
} from '@modelcontextprotocol/sdk/types.js';
|
||||
|
||||
export type MCPletType = 'read' | 'prepare' | 'action';
|
||||
export type Visibility = 'model' | 'app';
|
||||
|
||||
export interface MCPletToolAuth {
|
||||
required: 'passkey';
|
||||
enforcement: 'strict' | 'workflow' | 'host-only';
|
||||
promptMessage?: string;
|
||||
}
|
||||
|
||||
export interface MCPletToolDef {
|
||||
name: string;
|
||||
description: string;
|
||||
inputSchema: Record<string, unknown>;
|
||||
mcpletType: MCPletType;
|
||||
pool?: string;
|
||||
visibility: Visibility[];
|
||||
auth?: MCPletToolAuth;
|
||||
handler: (args: Record<string, unknown>, authPayload?: Record<string, unknown>) => Promise<unknown>;
|
||||
}
|
||||
|
||||
export class MCPletServer {
|
||||
private readonly server: Server;
|
||||
private readonly tools = new Map<string, MCPletToolDef>();
|
||||
|
||||
constructor(private readonly serverName: string, version = '0.1.0') {
|
||||
this.server = new Server(
|
||||
{ name: serverName, version },
|
||||
{ capabilities: { tools: {} } },
|
||||
);
|
||||
this.setupHandlers();
|
||||
}
|
||||
|
||||
registerTool(def: MCPletToolDef): this {
|
||||
this.tools.set(def.name, def);
|
||||
return this;
|
||||
}
|
||||
|
||||
async listen(): Promise<void> {
|
||||
const transport = new StdioServerTransport();
|
||||
await this.server.connect(transport);
|
||||
console.error(`[mcplet-server] ${this.serverName} listening on stdio`);
|
||||
}
|
||||
|
||||
private setupHandlers(): void {
|
||||
// tools/list — include _meta for MCPlet discovery (Spec Section 5.3.1)
|
||||
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
||||
tools: [...this.tools.values()].map((t) => ({
|
||||
name: t.name,
|
||||
description: t.description,
|
||||
inputSchema: t.inputSchema,
|
||||
_meta: {
|
||||
mcpletType: t.mcpletType,
|
||||
pool: t.pool,
|
||||
visibility: t.visibility,
|
||||
auth: t.auth ?? null,
|
||||
},
|
||||
})),
|
||||
}));
|
||||
|
||||
// tools/call
|
||||
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||
const toolName = request.params.name;
|
||||
const tool = this.tools.get(toolName);
|
||||
|
||||
if (!tool) {
|
||||
return {
|
||||
content: [{ type: 'text', text: this.errorEnvelope(toolName, `Tool "${toolName}" not found`, 'NOT_FOUND') }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Extract auth payload injected by Host (Spec Section 7.3.1)
|
||||
const requestMeta = request.params._meta as Record<string, unknown> | undefined;
|
||||
const authPayload = requestMeta?.mcplet_auth as Record<string, unknown> | undefined;
|
||||
|
||||
// Enforce auth requirement ('strict' = per-call ceremony; 'workflow' = cached ceremony per contextId)
|
||||
if (tool.auth?.required === 'passkey' &&
|
||||
(tool.auth.enforcement === 'strict' || tool.auth.enforcement === 'workflow') &&
|
||||
!authPayload) {
|
||||
return {
|
||||
content: [{ type: 'text', text: this.errorEnvelope(toolName, 'Authentication required', 'AUTH_REQUIRED', tool.mcpletType) }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const args = (request.params.arguments ?? {}) as Record<string, unknown>;
|
||||
const result = await tool.handler(args, authPayload);
|
||||
const envelope = {
|
||||
result,
|
||||
_meta: {
|
||||
timestamp: new Date().toISOString(),
|
||||
toolId: toolName,
|
||||
mcpletType: tool.mcpletType,
|
||||
visibility: tool.visibility,
|
||||
},
|
||||
};
|
||||
return { content: [{ type: 'text', text: JSON.stringify(envelope) }] };
|
||||
} catch (err) {
|
||||
return {
|
||||
content: [{ type: 'text', text: this.errorEnvelope(toolName, (err as Error).message, 'UNKNOWN_ERROR', tool.mcpletType) }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private errorEnvelope(toolId: string, message: string, code: string, mcpletType: MCPletType = 'read'): string {
|
||||
return JSON.stringify({
|
||||
error: { message, code },
|
||||
_meta: { timestamp: new Date().toISOString(), toolId, mcpletType, visibility: ['model'] },
|
||||
});
|
||||
}
|
||||
}
|
||||
71
reference_impl/mcplets/media-pool/email/index.ts
Normal file
71
reference_impl/mcplets/media-pool/email/index.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Email MCPlet
|
||||
* pool: media-pool | mcpletType: action | visibility: [app] | auth: passkey workflow
|
||||
*
|
||||
* visibility: ['app'] means this tool is NOT exposed to the LLM directly.
|
||||
* It is invoked only through the Host-controlled action-dispatch path
|
||||
* (Dispatch Agent) after Passkey authentication.
|
||||
* enforcement: 'workflow' — one Passkey ceremony per Director cycle (contextId),
|
||||
* not one per individual email send.
|
||||
*/
|
||||
import { MCPletServer } from '../../mcplet-server.js';
|
||||
|
||||
const MOCK_SERVICE_URL = process.env.MOCK_SERVICE_URL ?? 'http://localhost:5100';
|
||||
|
||||
const server = new MCPletServer('email-mcplet');
|
||||
|
||||
server.registerTool({
|
||||
name: 'send_email',
|
||||
description: '指定した宛先にメールを送信します。承認済みキャンペーン通知や予約リマインダーの送信に使用します。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
to: {
|
||||
type: 'string',
|
||||
description: '送信先メールアドレス',
|
||||
},
|
||||
subject: {
|
||||
type: 'string',
|
||||
description: 'メール件名',
|
||||
},
|
||||
body: {
|
||||
type: 'string',
|
||||
description: 'メール本文',
|
||||
},
|
||||
customerId: {
|
||||
type: 'string',
|
||||
description: '顧客ID (任意)',
|
||||
},
|
||||
},
|
||||
required: ['to', 'subject', 'body'],
|
||||
},
|
||||
mcpletType: 'action',
|
||||
pool: 'media-pool',
|
||||
visibility: ['app'],
|
||||
auth: {
|
||||
required: 'passkey',
|
||||
enforcement: 'workflow',
|
||||
promptMessage: 'このキャンペーンのメール一括送信を承認してください',
|
||||
},
|
||||
handler: async (args, authPayload) => {
|
||||
// In production: verify authPayload against FIDO2 server here.
|
||||
// For demo: log and proceed if authPayload present.
|
||||
if (authPayload) {
|
||||
console.error(`[email-mcplet] Auth verified (challenge: ${authPayload['challenge'] ?? 'n/a'})`);
|
||||
}
|
||||
|
||||
const res = await fetch(`${MOCK_SERVICE_URL}/email/send`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
to: args['to'],
|
||||
subject: args['subject'],
|
||||
body: args['body'],
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new Error(`Email service returned ${res.status}`);
|
||||
return res.json();
|
||||
},
|
||||
});
|
||||
|
||||
await server.listen();
|
||||
35
reference_impl/mcplets/media-pool/site-access/index.ts
Normal file
35
reference_impl/mcplets/media-pool/site-access/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* サイトアクセス MCPlet
|
||||
* pool: media-pool | mcpletType: read | visibility: [model]
|
||||
*/
|
||||
import { MCPletServer } from '../../mcplet-server.js';
|
||||
|
||||
const MOCK_SERVICE_URL = process.env.MOCK_SERVICE_URL ?? 'http://localhost:5100';
|
||||
|
||||
const server = new MCPletServer('site-access-mcplet');
|
||||
|
||||
server.registerTool({
|
||||
name: 'read_site_stats',
|
||||
description: 'EPARKサイトのアクセス統計を取得します。予約ページの閲覧数やコンバージョン率などを確認できます。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
period: {
|
||||
type: 'string',
|
||||
enum: ['last_7_days', 'last_30_days', 'today'],
|
||||
description: '集計期間',
|
||||
},
|
||||
},
|
||||
required: [],
|
||||
},
|
||||
mcpletType: 'read',
|
||||
pool: 'media-pool',
|
||||
visibility: ['model'],
|
||||
handler: async (_args) => {
|
||||
const res = await fetch(`${MOCK_SERVICE_URL}/site/stats`);
|
||||
if (!res.ok) throw new Error(`Site stats API returned ${res.status}`);
|
||||
return res.json();
|
||||
},
|
||||
});
|
||||
|
||||
await server.listen();
|
||||
60
reference_impl/mcplets/media-pool/sns/index.ts
Normal file
60
reference_impl/mcplets/media-pool/sns/index.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* SNS MCPlet
|
||||
* pool: media-pool | mcpletType: action | visibility: [app] | auth: passkey workflow
|
||||
*/
|
||||
import { MCPletServer } from '../../mcplet-server.js';
|
||||
|
||||
const MOCK_SERVICE_URL = process.env.MOCK_SERVICE_URL ?? 'http://localhost:5100';
|
||||
|
||||
const server = new MCPletServer('sns-mcplet');
|
||||
|
||||
server.registerTool({
|
||||
name: 'post_sns',
|
||||
description: 'SNSプラットフォームに投稿します。承認済みキャンペーン告知の拡散に使用します。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
platform: {
|
||||
type: 'string',
|
||||
enum: ['twitter', 'instagram', 'line'],
|
||||
description: '投稿するSNSプラットフォーム',
|
||||
},
|
||||
content: {
|
||||
type: 'string',
|
||||
description: '投稿内容',
|
||||
},
|
||||
scheduleAt: {
|
||||
type: 'string',
|
||||
description: '予約投稿日時 (ISO 8601形式、任意)',
|
||||
},
|
||||
},
|
||||
required: ['platform', 'content'],
|
||||
},
|
||||
mcpletType: 'action',
|
||||
pool: 'media-pool',
|
||||
visibility: ['app'],
|
||||
auth: {
|
||||
required: 'passkey',
|
||||
enforcement: 'workflow',
|
||||
promptMessage: 'このキャンペーンのSNS投稿を承認してください',
|
||||
},
|
||||
handler: async (args, authPayload) => {
|
||||
if (authPayload) {
|
||||
console.error(`[sns-mcplet] Auth verified (challenge: ${authPayload['challenge'] ?? 'n/a'})`);
|
||||
}
|
||||
|
||||
const res = await fetch(`${MOCK_SERVICE_URL}/sns/post`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
platform: args['platform'],
|
||||
content: args['content'],
|
||||
scheduleAt: args['scheduleAt'],
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new Error(`SNS service returned ${res.status}`);
|
||||
return res.json();
|
||||
},
|
||||
});
|
||||
|
||||
await server.listen();
|
||||
Reference in New Issue
Block a user