- 预览
- 代码
- 文档
复制
/**
* MCP Knowledge Retrieval using AG-Kit Agents
*/
import { Agent, OpenAIProvider } from "@ag-kit/agents";
import { createMCPToolkit } from "@ag-kit/tools";
/**
* Create MCP Knowledge Retrieval agent
*/
export async function createMCPKnowledgeRetrievalAgent(): Promise<{
agent: Agent;
cleanup: () => Promise<void>;
}> {
if (!process.env.OPENAI_API_KEY) {
throw new Error("OPENAI_API_KEY is required");
}
const provider = new OpenAIProvider({
apiKey: process.env.OPENAI_API_KEY!,
defaultModel: process.env.OPENAI_MODEL || "gpt-4o-mini",
baseURL: process.env.OPENAI_BASE_URL,
});
// Create MCP toolkit with AG-Kit Documentation server
const mcpToolkit = await createMCPToolkit(
{
"agkit-docs": {
url: "https://agkit.mintlify.app/mcp",
},
},
"agkit-docs-toolkit"
);
const agent = new Agent({
name: "mcp-knowledge-retrieval-agent",
description: "AI assistant that can search AG-Kit documentation using MCP",
model: provider,
instructions: `You are a helpful AI assistant with access to AG-Kit documentation through MCP (Model Context Protocol).
Your capabilities:
- Search and retrieve information from AG-Kit documentation
- Answer questions about AG-Kit features, APIs, and usage
- Provide code examples and implementation guidance
- Help users understand AG-Kit concepts and best practices
When users ask questions about AG-Kit, use the available MCP tools to search the documentation and provide accurate, helpful responses with proper source attribution.
Always be helpful, accurate, and provide relevant examples when possible.
When providing your answers, always format your output using Markdown. At the end of your response, include a list of reference documents you used or quoted from, formatted as a **numbered** Markdown list under the heading "References".`,
modelSettings: {
temperature: 0.5,
maxTokens: 16384,
},
tools: mcpToolkit.getTools(),
});
return {
agent,
cleanup: async () => {
await mcpToolkit.cleanup();
},
};
}
// No pre-created agent instance to avoid environment variable issues
MCP + 知识检索 - AG-Kit Agents
本演示展示
本演示展示了 AG-Kit 的 MCP(Model Context Protocol)知识检索功能:- MCP 集成:使用 Model Context Protocol 连接到外部知识源
- AG-Kit 文档搜索:通过 MCP 服务器搜索 AG-Kit 文档
- 实时知识检索:从文档中检索最新信息
- 上下文响应:提供带有来源归属的准确答案
- 可流式 HTTP 传输:使用可流式 HTTP 进行高效数据传输
如何交互
尝试这些建议以探索 MCP 知识检索:- “什么是 AG-Kit?“(搜索 AG-Kit 概览和介绍)
- “AG-Kit 支持哪些框架?“(查找支持的框架)
- “AG-Kit 与其他 Agent 的区别是什么?“(比较 AG-Kit 与替代方案)
- “如何创建新的 Agent?“(查找 Agent 创建文档)
- “给我看一些 Human in the Loop 的例子”(搜索 HITL 示例)
技术实现
后端 (AG-Kit Agents):- 用于模型集成的带有
OpenAIProvider的Agent类 - 用于 MCP 服务器连接的
createMCPToolkit - AG-Kit 文档 MCP 服务器位于
https://agkit.mintlify.app/mcp - 用于高效数据流式传输的可流式 HTTP 传输
- 带有 MCP 功能的内置工具集成
useChat钩子管理对话状态AgKitChat组件提供聊天界面- 专门用于文档查询的建议
- 欢迎消息指导用户询问 AG-Kit 文档
- URL 参数在不同 Agent 实现之间切换
关键特性
- 实时文档访问:实时访问 AG-Kit 文档
- 智能搜索:跨文档的 MCP 驱动搜索
- 来源归属:响应包含适当的来源引用
- 上下文理解:为后续问题保持对话上下文
- 高效流式传输:可流式 HTTP 传输以获得快速响应
MCP 优势
- 标准化协议:使用 Model Context Protocol 实现一致的知识访问
- 可扩展:易于添加更多 MCP 服务器以访问不同的知识源
- 实时更新:始终访问最新文档
- 高效:优化的数据传输和缓存