/**
* 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