-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add sub-agent MCP server with background processing support #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # Sub-Agent MCP Server | ||
|
|
||
| A Model Context Protocol (MCP) server that provides chat completion capabilities with background processing support. | ||
|
|
||
| ## Features | ||
|
|
||
| - **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 | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Building | ||
|
|
||
| ```bash | ||
| cd sub-agent | ||
| go build -o sub-agent . | ||
| ``` | ||
|
|
||
| ### Running | ||
|
|
||
| ```bash | ||
| ./sub-agent | ||
| ``` | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| | 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` | | ||
|
|
||
| ## Tools | ||
|
|
||
| ### `sub_agent_chat` | ||
|
|
||
| Send a chat completion message to OpenAI. | ||
|
|
||
| **Inputs:** | ||
| - `message` (string, required): The message to send to the AI model | ||
| - `background` (boolean, optional): Whether to process in background (default: false) | ||
|
|
||
| **Outputs:** | ||
| - If `background` is false: Returns the AI response immediately | ||
| - If `background` is true: Returns a task ID for later retrieval | ||
|
|
||
| **Example (synchronous):** | ||
| ```json | ||
| { | ||
| "message": "What is the capital of France?" | ||
| } | ||
| ``` | ||
|
|
||
| **Example (background):** | ||
| ```json | ||
| { | ||
| "message": "Analyze this long document...", | ||
| "background": true | ||
| } | ||
| ``` | ||
|
|
||
| ### `sub_agent_list` | ||
|
|
||
| List all active background sub-agent calls. | ||
|
|
||
| **Inputs:** None | ||
|
|
||
| **Outputs:** List of task objects with task_id, created_at, status, and message | ||
|
|
||
| **Example:** | ||
| ```json | ||
| [ | ||
| { | ||
| "task_id": "task-1234567890", | ||
| "created_at": "2024-01-01T12:00:00Z", | ||
| "status": "completed", | ||
| "message": "What is the capital of France?" | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| ### `sub_agent_get_result` | ||
|
|
||
| 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:** | ||
| ```json | ||
| { | ||
| "task_id": "task-1234567890" | ||
| } | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### With a MCP Client | ||
|
|
||
| Configure your MCP client to use the sub-agent server: | ||
|
|
||
| ```json | ||
| { | ||
| "mcpServers": { | ||
| "sub-agent": { | ||
| "command": "./sub-agent", | ||
| "args": [], | ||
| "env": { | ||
| "OPENAI_API_KEY": "your-api-key", | ||
| "OPENAI_MODEL": "gpt-4o-mini", | ||
| "SUB_AGENT_TTL": "4" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Direct API Usage | ||
|
|
||
| ```bash | ||
| # Build and run the server | ||
| go build -o sub-agent . | ||
| ./sub-agent | ||
|
|
||
| # Or run directly | ||
| go run main.go | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| The server maintains an in-memory store of background tasks with the following characteristics: | ||
|
|
||
| 1. **Task Storage**: Concurrent map with read-write mutex for thread safety | ||
| 2. **TTL Management**: Automatic cleanup of expired tasks every hour | ||
| 3. **Background Processing**: Goroutines for asynchronous task execution | ||
| 4. **Status Tracking**: Tasks track their status (pending, completed, expired) | ||
|
|
||
| ## License | ||
|
|
||
| MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| module github.com/mudler/MCPs/sub-agent | ||
|
|
||
| go 1.24.7 | ||
|
|
||
| require github.com/modelcontextprotocol/go-sdk v1.0.0 | ||
|
|
||
| require ( | ||
| github.com/google/jsonschema-go v0.3.0 // indirect | ||
| github.com/yosida95/uritemplate/v3 v3.0.2 // indirect | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= | ||
| github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= | ||
| github.com/google/jsonschema-go v0.3.0 h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q= | ||
| github.com/google/jsonschema-go v0.3.0/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE= | ||
| github.com/modelcontextprotocol/go-sdk v1.0.0 h1:Z4MSjLi38bTgLrd/LjSmofqRqyBiVKRyQSJgw8q8V74= | ||
| github.com/modelcontextprotocol/go-sdk v1.0.0/go.mod h1:nYtYQroQ2KQiM0/SbyEPUWQ6xs4B95gJjEalc9AQyOs= | ||
| github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= | ||
| github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= | ||
| golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo= | ||
| golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg= |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for a separate go.mod/go.sum. there is already one at top level