# Agent Functions Module
This module provides a modular, maintainable architecture for the MCP Plan Iterator System.
## π File Structure
```
agent_functions/
βββ index.js # Main exports and factory function
βββ mcpClient.js # MCP session management and communication
βββ llmClient.js # LLM provider abstraction (DMR, Ollama, OpenAI)
βββ planExecutor.js # Plan creation and execution logic
βββ routeHandlers.js # Express route handlers
βββ README.md # This file
```
## π§© Components
### **McpClient** (`mcpClient.js`)
- Handles MCP session initialization
- Manages tool/resource catalogs
- Provides MCP communication methods
- Abstracts away MCP protocol details
### **LlmClient** (`llmClient.js`)
- Supports multiple LLM providers (DMR, Ollama, OpenAI)
- Handles tool schema sanitization
- Provides unified interface for LLM calls
- Context-aware tool execution
### **PlanExecutor** (`planExecutor.js`)
- Creates execution plans using available tools
- Reads and updates planner resources
- Executes complete workflows programmatically
- Handles both automatic and manual step execution
### **RouteHandlers** (`routeHandlers.js`)
- Express route handlers for `/api/plan` and `/api/chat`
- Clean separation of HTTP concerns from business logic
- Error handling and response formatting
### **Factory Function** (`index.js`)
- `createAgentSystem(config)` - One-stop initialization
- Returns fully configured system components
- Handles dependency injection
## π Usage
```javascript
import { createAgentSystem } from './agent_functions/index.js';
const agentSystem = await createAgentSystem({
mcpEndpoint: "http://localhost:4000/mcp",
auth: "Bearer local-dev",
strategy: "DMR", // or "Ollama", "OpenAI"
dockerModelRunnerUrl: process.env.DOCKER_MODEL_RUNNER_URL,
ollamaUrl: process.env.OLLAMA_URL,
openaiApiKey: process.env.OPENAI_API_KEY
});
// Use the components
app.post("/api/chat", agentSystem.routeHandlers.handleChatRoute);
app.post("/api/plan", agentSystem.routeHandlers.handlePlanRoute);
```
## β¨ Benefits
- **Maintainability**: Each module has a single responsibility
- **Testability**: Components can be tested in isolation
- **Extensibility**: Easy to add new LLM providers or features
- **Readability**: Clear separation of concerns
- **Reusability**: Components can be used independently
## π Workflow
1. **User Query** β `handleChatRoute`
2. **Plan Creation** β `PlanExecutor.createExecutionPlan()`
3. **Plan Reading** β `PlanExecutor.readCurrentPlan()`
4. **Execution Loop** β `PlanExecutor.executeCompleteWorkflow()`
5. **Tool Calls** β `McpClient.callMcp()` + `LlmClient.executeLLMWithTool()`
6. **Response** β Rich execution summary with step details
The old monolithic `index.js` (651 lines) is now a clean 50-line file that orchestrates modular components!