- Preview
- Code
- Doc
Copy
/**
* 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 + Knowledge Retrieval - AG-Kit Agents (TypeScript)
What This Demo Shows
This demo showcases AG-Kit’s MCP (Model Context Protocol) knowledge retrieval capabilities:- MCP Integration: Uses Model Context Protocol to connect to external knowledge sources
- AG-Kit Documentation Search: Searches AG-Kit documentation through MCP server
- Real-time Knowledge Retrieval: Retrieves up-to-date information from documentation
- Contextual Responses: Provides accurate answers with source attribution
- Streamable HTTP Transport: Uses streamable HTTP for efficient data transfer
How to Interact
Try these suggestions to explore MCP knowledge retrieval:- “What is AG-Kit?” (searches for AG-Kit overview and introduction)
- “Which frameworks does AG-Kit support?” (finds supported frameworks)
- “What is the difference between AG-Kit and other agents?” (compares AG-Kit with alternatives)
- “How do I create a new agent?” (finds agent creation documentation)
- “Show me examples of human-in-the-loop” (searches for HITL examples)
Technical Implementation
Backend (AG-Kit Agents):Agentclass withOpenAIProviderfor model integrationcreateMCPToolkitfor MCP server connection- AG-Kit Documentation MCP server at
https://agkit.mintlify.app/mcp - Streamable HTTP transport for efficient data streaming
- Built-in tool integration with MCP capabilities
useChathook manages conversation stateAgKitChatcomponent provides chat interface- Specialized suggestions for documentation queries
- Welcome message guides users to ask about AG-Kit docs
- URL parameter switching for different agent implementations
Key Features
- Live Documentation Access: Real-time access to AG-Kit documentation
- Intelligent Search: MCP-powered search across documentation
- Source Attribution: Responses include proper source references
- Contextual Understanding: Maintains conversation context for follow-up questions
- Efficient Streaming: Streamable HTTP transport for fast responses
MCP Benefits
- Standardized Protocol: Uses Model Context Protocol for consistent knowledge access
- Extensible: Easy to add more MCP servers for different knowledge sources
- Real-time Updates: Always accesses the latest documentation
- Efficient: Optimized data transfer and caching