MCP Endpoints Bug Fixes
Issues Fixed
1. create_task - Missing created_by Field
Problem:
{"error": "pq: null value in column "created_by" of relation "tasks" violates not-null constraint"}
Root Cause:
The tasks table has a NOT NULL constraint on the created_by column, but the MCP handler's executeCreateTask function wasn't including this field in the INSERT statement.
Solution:
- Added
created_byparameter support to thecreate_tasktool - If
created_byis not provided, the function now automatically queries the first agent from the project - Updated the INSERT statement to include the
created_byfield - Updated the tool schema to document the
created_byparameter
Changes Made:
// Now accepts created_by parameter
createdBy, _ := args["created_by"].(string)
// Auto-assign first agent if not provided
if createdBy == "" {
err := h.db.QueryRow(`SELECT id FROM agents WHERE project_id = $1 LIMIT 1`, projectID).Scan(&createdBy)
if err != nil {
return `{"error": "created_by is required or no agents found in project"}`, true
}
}
// Include created_by in INSERT
query := `INSERT INTO tasks (id, project_id, title, description, status, priority, created_by, assigned_to)
VALUES ($1, $2, $3, $4, 'pending', $5, $6, $7) RETURNING id, created_at`
2. add_context - Malformed Array Literal for Tags
Problem:
{"error": "pq: malformed array literal: "["documentation","backend","onboarding"]""}
Root Cause:
The executeAddContext function was converting tags to JSON format using json.Marshal(), but PostgreSQL's TEXT[] column type expects PostgreSQL array literals, not JSON arrays. The difference:
- JSON format:
["tag1","tag2"] - PostgreSQL array format:
{tag1,tag2}
Solution:
- Added missing
agent_idrequirement (contexts table requires this field) - If
agent_idis not provided, automatically query the first agent from the project - Use
pq.Array()from thegithub.com/lib/pqpackage for proper PostgreSQL array handling - Added import for
"github.com/lib/pq"package
Changes Made:
// Import added
import "github.com/lib/pq"
// Handle agent_id requirement
agentID, _ := args["agent_id"].(string)
if agentID == "" {
err := h.db.QueryRow(`SELECT id FROM agents WHERE project_id = $1 LIMIT 1`, projectID).Scan(&agentID)
if err != nil {
return `{"error": "agent_id is required or no agents found in project"}`, true
}
}
// Convert tags properly for PostgreSQL
var tags []string
if tagsInterface, ok := args["tags"].([]interface{}); ok {
for _, tag := range tagsInterface {
if tagStr, ok := tag.(string); ok {
tags = append(tags, tagStr)
}
}
}
// Use pq.Array for proper PostgreSQL array handling
err := h.db.QueryRow(query, id, projectID, agentID, title, content, pq.Array(tags)).Scan(&createdAt)
Testing the Fixes
Test create_task:
With created_by (explicit):
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "create_task",
"arguments": {
"project_id": "your-project-id",
"title": "Test Task",
"description": "Testing create_task fix",
"priority": "medium",
"created_by": "agent-id"
}
}
}
Without created_by (auto-assigned):
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "create_task",
"arguments": {
"project_id": "your-project-id",
"title": "Test Task",
"description": "Testing auto-assign",
"priority": "high"
}
}
}
Test add_context:
With agent_id (explicit):
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "add_context",
"arguments": {
"project_id": "your-project-id",
"agent_id": "agent-id",
"title": "Testing Documentation",
"content": "This is a test context with proper array handling",
"tags": ["documentation", "backend", "testing"]
}
}
}
Without agent_id (auto-assigned):
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "add_context",
"arguments": {
"project_id": "your-project-id",
"title": "Auto-assigned Context",
"content": "Testing automatic agent assignment",
"tags": ["test", "fix"]
}
}
}
Files Modified
-
internal/mcp/handler.go
- Added
pqpackage import - Fixed
executeCreateTask()to handlecreated_byfield - Fixed
executeAddContext()to use proper PostgreSQL array format and handleagent_idfield - Updated tool schemas to document the new optional parameters
- Added
-
cmd/server/main.go (previous fix)
- Added CORS headers to health endpoint
Summary
Both issues have been resolved:
- ✅
create_tasknow properly handles thecreated_byconstraint - ✅
add_contextnow properly handles PostgreSQL array format for tags - ✅ Both functions automatically assign the first project agent if not explicitly provided
- ✅ Tool schemas updated to reflect the changes
The server has been successfully rebuilt and is ready for testing.