A Model Context Protocol (MCP) server that provides chat completion capabilities with background processing support.
- Chat Completion Tool: Send messages to OpenAI API with synchronous or asynchronous processing
- Background Processing: Execute chat completions in the background and retrieve results later
- Task Management: List active background tasks and get their results
- TTL-based Cleanup: Automatic cleanup of expired tasks based on configurable TTL
cd sub-agent
go build -o sub-agent ../sub-agent| Variable | Description | Default |
|---|---|---|
OPENAI_BASE_URL |
Base URL for OpenAI API | https://api.openai.com/v1 |
OPENAI_MODEL |
Model to use for completions | gpt-4o-mini |
OPENAI_API_KEY |
API key for OpenAI authentication | (required) |
SUB_AGENT_TTL |
TTL in hours for background task results | 4 |
Send a chat completion message to OpenAI.
Inputs:
message(string, required): The message to send to the AI modelbackground(boolean, optional): Whether to process in background (default: false)
Outputs:
- If
backgroundis false: Returns the AI response immediately - If
backgroundis true: Returns a task ID for later retrieval
Example (synchronous):
{
"message": "What is the capital of France?"
}Example (background):
{
"message": "Analyze this long document...",
"background": true
}List all active background sub-agent calls.
Inputs: None
Outputs: List of task objects with task_id, created_at, status, and message
Example:
[
{
"task_id": "task-1234567890",
"created_at": "2024-01-01T12:00:00Z",
"status": "completed",
"message": "What is the capital of France?"
}
]Get the result of a background task.
Inputs:
task_id(string, required): The task ID to get result for
Outputs: Task result or error if not found/expired
Example:
{
"task_id": "task-1234567890"
}Configure your MCP client to use the sub-agent server:
{
"mcpServers": {
"sub-agent": {
"command": "./sub-agent",
"args": [],
"env": {
"OPENAI_API_KEY": "your-api-key",
"OPENAI_MODEL": "gpt-4o-mini",
"SUB_AGENT_TTL": "4"
}
}
}
}# Build and run the server
go build -o sub-agent .
./sub-agent
# Or run directly
go run main.goThe server maintains an in-memory store of background tasks with the following characteristics:
- Task Storage: Concurrent map with read-write mutex for thread safety
- TTL Management: Automatic cleanup of expired tasks every hour
- Background Processing: Goroutines for asynchronous task execution
- Status Tracking: Tasks track their status (pending, completed, expired)
MIT