Open1
mcp sample
import * as dotenv from 'dotenv'; // 例: 環境変数を使用する場合のためにインポート(必須ではない)
// dotenv.config(); // 環境変数をロード
// 1. 架空のAPI情報ナレッジベースの型定義
interface ApiParameter {
name: string;
type: string;
required: boolean;
description: string;
}
interface ApiResponse {
type: string;
description: string;
}
interface ApiInfo {
name: string;
description: string;
parameters: ApiParameter[];
response: ApiResponse;
example: string;
}
// 2. 架空のAPI情報ナレッジベース (公開されていない情報として扱う)
// 実際のシステムではデータベースなどから取得します
const apiKnowledgeBase: ApiInfo[] = [
{
name: "GetUserProfile",
description: "ユーザーのプロフィール情報を取得するAPIです。ユーザーIDを指定します。",
parameters: [{ name: "userId", type: "string", required: true, description: "取得したいユーザーのID" }],
response: { type: "json", description: "ユーザー名、メールアドレス、最終ログイン日時などが含まれます。" },
example: "GET /users/{userId}/profile"
},
{
name: "CreateOrder",
description: "新しい注文を作成するAPIです。商品ID、数量、配送先情報を指定します。",
parameters: [
{ name: "productId", type: "string", required: true, description: "注文する商品のID" },
{ name: "quantity", type: "integer", required: true, description: "数量" },
{ name: "shippingAddress", type: "object", required: true, description: "配送先住所情報(住所、氏名など)" }
],
response: { type: "json", description: "作成された注文のIDとステータスを返します。" },
example: "POST /orders"
},
{
name: "ListProducts",
description: "登録されている商品リストを取得するAPIです。オプションでカテゴリやキーワードでフィルタリングできます。",
parameters: [
{ name: "category", type: "string", required: false, description: "商品のカテゴリ" },
{ name: "keyword", type: "string", required: false, description: "検索キーワード" }
],
response: { type: "json", description: "商品情報のリスト(商品ID、名前、価格、在庫など)を返します。" },
example: "GET /products"
}
];
// 3. コンテキスト提供機能 (MCPサーバーの役割をシミュレート)
function getApiContext(query: string): string {
/**
* ユーザーのクエリに関連するAPI情報をナレッジベースから検索・取得し、テキスト形式で返します。
* 本来は高度な検索や関連度判定が行われますが、ここでは単純なキーワードマッチングです。
*/
const lowerQueryWords = query.toLowerCase().split(/\s+/).filter(word => word.length > 0);
const relevantApis = apiKnowledgeBase.filter(api => {
const apiText = `${api.name} ${api.description}`.toLowerCase();
// クエリの単語がAPI情報に含まれているかチェック
return lowerQueryWords.some(word => apiText.includes(word));
});
// 取得したAPI情報をAIが理解しやすいテキスト形式に整形
let contextText = "利用可能なAPI情報:\n\n";
if (relevantApis.length === 0) {
contextText += "関連するAPI情報は見つかりませんでした。\n";
} else {
relevantApis.forEach(api => {
contextText += `--- API: ${api.name} ---\n`;
contextText += `説明: ${api.description}\n`;
contextText += "パラメータ:\n";
if (api.parameters && api.parameters.length > 0) {
api.parameters.forEach(param => {
contextText += ` - ${param.name} (${param.type}${param.required ? ', 必須' : ''}): ${param.description}\n`;
});
} else {
contextText += " (パラメータなし)\n";
}
contextText += `レスポンス: ${api.response.description}\n`;
contextText += `例: ${api.example}\n\n`;
});
}
return contextText;
}
// 4. AI連携のシミュレーション
// ユーザーのプログラミングに関するクエリを想定
const userProgrammingQuery1 = "ユーザー情報を取得するTypeScriptコードを書きたい";
// コンテキスト提供サーバーから関連API情報を取得
const apiContext1 = getApiContext(userProgrammingQuery1);
// AIへのプロンプトを生成(取得したコンテキストを含める)
// 実際のAI連携では、API呼び出しツールとしてAIにAPI情報を提供したり、
// より複雑な形式でコンテキストを渡したりします。
const aiPrompt1 = `
以下のAPI情報を参考に、プログラミングの要求に応じたコードを生成してください。
--- APIコンテキスト ---
${apiContext1}
---
ユーザーからのプログラミング要求:
${userProgrammingQuery1}
生成するコード:
`;
// 生成されたプロンプトを表示
console.log("--- AIへのプロンプト(MCPサーバーが提供するコンテキストを含む)---");
console.log(aiPrompt1);
// --- 別なクエリでのシミュレーション ---
const userProgrammingQuery2 = "新しい注文を作成するには?";
const apiContext2 = getApiContext(userProgrammingQuery2);
const aiPrompt2 = `
以下のAPI情報を参考に、プログラミングの要求に応じたコードを生成してください。
--- APIコンテキスト ---
${apiContext2}
---
ユーザーからのプログラミング要求:
${userProgrammingQuery2}
生成するコード:
`;
console.log("\n--- 別なクエリでのAIへのプロンプト ---");
console.log(aiPrompt2);
// --- 関連情報が見つからない場合のシミュレーション ---
const userProgrammingQuery3 = "天気情報を取得するには?"; // この情報はない
const apiContext3 = getApiContext(userProgrammingQuery3);
const aiPrompt3 = `
以下のAPI情報を参考に、プログラミングの要求に応じたコードを生成してください。
--- APIコンテキスト ---
${apiContext3}
---
ユーザーからのプログラミング要求:
${userProgrammingQuery3}
生成するコード:
`;
console.log("\n--- 関連情報が見つからない場合のプロンプト ---");
console.log(aiPrompt3);