Files
kandev/apps/backend/internal/mcp/server/handlers_test.go
T
João Salavisa 0b7bbd93d5 feat(mcp): accept repository_url and local_path on add_branch_to_task_kandev (#1256)
* feat(mcp): accept repository_url and local_path on add_branch_to_task_kandev

Mirror create_task_kandev's repo-locator surface so agents can attach a
branch without first looking up the repository UUID. The MCP tool now
takes repository_id OR repository_url OR local_path; the service
resolves alternatives via the same workspace-scoped find-or-create
path used by create_task.

* fix(mcp): run executor guard before repo resolution on add_branch_to_task

ResolveRepositoryRef may FindOrCreateRepository / CreateRepository
inside the github_url / local_path paths. Running it before
requireWorktreeExecutorForBranchAdd let a rejected add_branch call
on a docker/sprites/SSH task leak an orphan Repository row into
the workspace.

Move the executor check ahead of resolution and add a regression
test that asserts ListRepositories count is unchanged after a
rejected call with a brand-new github_url.

* test(mcp): assert task_id default in add_branch local_path test

* fix(mcp): reject multiple repo locators on add_branch_to_task

resolveRepoInput applies a silent precedence (repository_id >
github_url > local_path), so an agent that accidentally supplies
two of the three locators got a behaviour change with no signal.
Reject at the WS handler with a clear error so the mistake is
surfaced.

Also refresh the AddBranchToTask docstring to document the new
LocalPath / GitHubURL inputs and to correct the workspace-membership
check ownership (now enforced by ResolveRepositoryRef → resolveRepoInput
for the alternative paths).

* fix(mcp): classify resolve_repository errors as validation, not internal

Failures from ResolveRepositoryRef are user-fixable input mistakes
(malformed URL, unknown local_path, cross-workspace repository_id).
Without a matching case the wrapper "resolve repository: ..." message
fell through to ErrorCodeInternalError, so an agent that supplied
e.g. a bitbucket URL saw a server-fault code instead of a validation
error it could correct.

* fix(mcp): narrow add_branch error classification + use wire field name

Two follow-ups to the round-3/4 review feedback:

- Mutual-exclusion error mentioned the MCP tool alias `repository_url`
  but the WS handler sees the wire field `github_url`. A direct WS
  caller would see an error naming a field it never passed.
- The broad `"resolve repository: "` prefix classified ALL wrapped
  errors as Validation, including DB / system failures from
  CreateRepository / ListRepositories inside ResolveRepositoryRef.
  Narrow to specific user-fixable patterns (GitHub URL parse failures,
  cross-workspace repository_id) so genuine internal errors no longer
  masquerade as validation rejections.

* fix(mcp): mutual-exclusion check at MCP tier names agent-facing alias

WS-tier rejection works for direct WS callers but its error mentions
github_url, the wire field — confusing for MCP agents that supplied
repository_url. Move the check up to addBranchToTaskHandler so the
error reads "pass at most one of repository_id, repository_url,
local_path" using the names the agent actually typed. WS handler
still validates as defense-in-depth.

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-06-03 03:04:04 +01:00

496 lines
17 KiB
Go

package mcp
import (
"encoding/json"
"testing"
ws "github.com/kandev/kandev/pkg/websocket"
"github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTaskModeServer(t *testing.T, backend BackendClient, taskID string) *Server {
t.Helper()
log := newTestLogger(t)
return New(backend, "test-session", taskID, 10005, log, "", false, ModeTask)
}
func TestCreateTask_ToolSchema_HasParentID(t *testing.T) {
backend := &testBackend{}
s := newTaskModeServer(t, backend, "task-current")
toolsMap := s.mcpServer.ListTools()
tool, ok := toolsMap["create_task_kandev"]
require.True(t, ok, "create_task tool not registered")
schema, err := json.Marshal(tool.Tool.InputSchema)
require.NoError(t, err)
var parsed map[string]interface{}
require.NoError(t, json.Unmarshal(schema, &parsed))
props, ok := parsed["properties"].(map[string]interface{})
require.True(t, ok, "schema should have properties")
assert.Contains(t, props, "parent_id", "create_task schema must expose parent_id")
assert.Contains(t, props, "title")
assert.Contains(t, props, "workspace_id")
assert.Contains(t, props, "workflow_id")
// parent_id, workspace_id, workflow_id, workflow_step_id should NOT be required
required, _ := parsed["required"].([]interface{})
requiredSet := make(map[string]bool)
for _, r := range required {
requiredSet[r.(string)] = true
}
assert.True(t, requiredSet["title"], "title should be required")
assert.False(t, requiredSet["parent_id"], "parent_id should not be required")
assert.False(t, requiredSet["workspace_id"], "workspace_id should not be required")
assert.False(t, requiredSet["workflow_id"], "workflow_id should not be required")
}
func TestCreateTask_SelfResolvesToTaskID(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "subtask-1", "parent_id": "task-current"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Write tests",
"parent_id": "self",
})
assert.False(t, result.IsError)
assert.Equal(t, ws.ActionMCPCreateTask, backend.lastAction)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "task-current", payload["parent_id"], "self should resolve to current task ID")
assert.Equal(t, "Write tests", payload["title"])
assert.Equal(t, "task-current", payload["source_task_id"], "source_task_id should be set to current task")
assert.Equal(t, true, payload["start_agent"], "start_agent should default to true")
}
func TestCreateTask_SelfWithNoTaskContext_ReturnsError(t *testing.T) {
backend := &testBackend{}
s := newTaskModeServer(t, backend, "")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Write tests",
"parent_id": "self",
})
assert.True(t, result.IsError)
}
func TestCreateTask_ExplicitParentID(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "subtask-1", "parent_id": "task-abc"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Fix bug",
"parent_id": "task-abc",
})
assert.False(t, result.IsError)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "task-abc", payload["parent_id"])
}
func TestCreateTask_NoParentID_WithIDs_CreatesTopLevelTask(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new", "title": "Standalone"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Standalone",
"workspace_id": "ws-1",
"workflow_id": "wf-1",
})
assert.False(t, result.IsError)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "", payload["parent_id"])
assert.Equal(t, "ws-1", payload["workspace_id"])
assert.Equal(t, "wf-1", payload["workflow_id"])
assert.Equal(t, "task-current", payload["source_task_id"])
}
func TestCreateTask_SourceTaskID_AlwaysSet(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new"},
}
s := newTaskModeServer(t, backend, "my-task-123")
callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "New task",
"workspace_id": "ws-1",
"workflow_id": "wf-1",
})
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "my-task-123", payload["source_task_id"])
}
func TestCreateTask_SourceTaskID_EmptyWhenNoTaskContext(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new"},
}
s := newTaskModeServer(t, backend, "")
callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "New task",
"workspace_id": "ws-1",
"workflow_id": "wf-1",
})
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "", payload["source_task_id"])
}
func TestCreateTask_StartAgentFalse_DoesNotAutoStart(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new", "title": "Plan task"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Plan task",
"workspace_id": "ws-1",
"workflow_id": "wf-1",
"start_agent": false,
})
assert.False(t, result.IsError)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, false, payload["start_agent"], "start_agent should be false when explicitly set")
}
func TestCreateTask_WithRepositoryID(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new", "title": "Task with repo"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Task with repo",
"workspace_id": "ws-1",
"workflow_id": "wf-1",
"repository_id": "repo-123",
"base_branch": "main",
})
assert.False(t, result.IsError)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
repos, ok := payload["repositories"].([]map[string]string)
require.True(t, ok, "repositories should be a slice")
require.Len(t, repos, 1)
assert.Equal(t, "repo-123", repos[0]["repository_id"])
assert.Equal(t, "main", repos[0]["base_branch"])
}
func TestCreateTask_WithLocalPath(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new", "title": "Task with local path"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Task with local path",
"workspace_id": "ws-1",
"workflow_id": "wf-1",
"local_path": "/Users/me/projects/myrepo",
})
assert.False(t, result.IsError)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
repos, ok := payload["repositories"].([]map[string]string)
require.True(t, ok, "repositories should be a slice")
require.Len(t, repos, 1)
assert.Equal(t, "/Users/me/projects/myrepo", repos[0]["local_path"])
}
func TestCreateTask_WithRepositoryURL(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new", "title": "Task with URL"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Task with URL",
"workspace_id": "ws-1",
"workflow_id": "wf-1",
"repository_url": "https://github.com/acme/widgets",
"base_branch": "main",
})
assert.False(t, result.IsError)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
repos, ok := payload["repositories"].([]map[string]string)
require.True(t, ok, "repositories should be a slice")
require.Len(t, repos, 1)
assert.Equal(t, "https://github.com/acme/widgets", repos[0]["github_url"])
assert.Equal(t, "main", repos[0]["base_branch"])
}
// TestCreateTask_BaseBranchOnly_ForwardsTopLevel pins the bug-fix wiring:
// when the caller passes only base_branch (no repository_id / local_path /
// repository_url), the MCP server forwards it at the top level of the WS
// payload so the backend can apply it as an override on inherited
// subtask repos. Previously base_branch was silently dropped when no
// repo identifier was passed.
func TestCreateTask_BaseBranchOnly_ForwardsTopLevel(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "subtask-1", "parent_id": "task-current"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Stacked PR child",
"parent_id": "self",
"description": "branch off the parent's PR branch",
"base_branch": "feature/create-new-page-endp-05z",
})
assert.False(t, result.IsError)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "feature/create-new-page-endp-05z", payload["base_branch"],
"base_branch should be forwarded at the top level even when no repo identifier is supplied")
_, hasRepos := payload["repositories"]
assert.False(t, hasRepos, "no repositories slice should be produced when only base_branch is supplied")
}
func TestCreateTask_RepositoryURL_AllowedForSubtasks(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new", "title": "Subtask with URL"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Subtask with URL",
"parent_id": "self",
"description": "Fix the upstream review-eligibility check",
"repository_url": "https://github.com/acme/widgets",
"base_branch": "main",
})
assert.False(t, result.IsError, "repository_url should be accepted for subtasks (cross-repo subtask)")
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "task-current", payload["parent_id"], "self resolves to current task id")
repos, ok := payload["repositories"].([]map[string]string)
require.True(t, ok, "repositories should be a slice")
require.Len(t, repos, 1)
assert.Equal(t, "https://github.com/acme/widgets", repos[0]["github_url"])
assert.Equal(t, "main", repos[0]["base_branch"])
}
func TestCreateTask_LocalPath_AllowedForSubtasks(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "task-new", "title": "Subtask with local path"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "create_task_kandev", map[string]interface{}{
"title": "Subtask with local path",
"parent_id": "self",
"description": "Patch the sibling repo",
"local_path": "/Users/me/projects/sibling",
})
assert.False(t, result.IsError, "local_path should be accepted for subtasks (cross-repo subtask)")
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "task-current", payload["parent_id"])
repos, ok := payload["repositories"].([]map[string]string)
require.True(t, ok)
require.Len(t, repos, 1)
assert.Equal(t, "/Users/me/projects/sibling", repos[0]["local_path"])
}
// TestAddBranchToTask_ForwardsRepositoryURL verifies the agent-facing alias:
// repository_url on the MCP tool surface translates to github_url on the WS
// payload — mirroring create_task_kandev's wire format so the backend handler
// can resolve through the same code path.
func TestAddBranchToTask_ForwardsRepositoryURL(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "tr-1", "task_id": "task-current"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "add_branch_to_task_kandev", map[string]interface{}{
"repository_url": "https://github.com/acme/widgets",
"checkout_branch": "feature/x",
})
assert.False(t, result.IsError)
assert.Equal(t, ws.ActionMCPAddBranchToTask, backend.lastAction)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "task-current", payload["task_id"], "task_id should default to current task")
assert.Equal(t, "https://github.com/acme/widgets", payload["github_url"],
"repository_url should be forwarded as github_url to match create_task wire format")
assert.Equal(t, "feature/x", payload["checkout_branch"])
assert.Equal(t, "", payload["repository_id"])
}
// TestAddBranchToTask_ForwardsLocalPath verifies local_path is plumbed through
// to the WS payload so the backend can find-or-create the repo in the task's
// workspace.
func TestAddBranchToTask_ForwardsLocalPath(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{"id": "tr-1", "task_id": "task-current"},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "add_branch_to_task_kandev", map[string]interface{}{
"local_path": "/Users/me/projects/sibling",
"checkout_branch": "feature/y",
})
assert.False(t, result.IsError)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "task-current", payload["task_id"], "task_id should default to current task")
assert.Equal(t, "/Users/me/projects/sibling", payload["local_path"])
assert.Equal(t, "feature/y", payload["checkout_branch"])
assert.Equal(t, "", payload["repository_id"])
}
// TestAddBranchToTask_RejectsMultipleLocators verifies the MCP-tier
// mutual-exclusion check fires before the request hits the WS handler, so
// the error names the agent-facing alias (repository_url) instead of the
// wire field (github_url).
func TestAddBranchToTask_RejectsMultipleLocators(t *testing.T) {
backend := &testBackend{}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "add_branch_to_task_kandev", map[string]interface{}{
"repository_url": "https://github.com/acme/widgets",
"local_path": "/Users/me/projects/sibling",
})
assert.True(t, result.IsError, "passing both repository_url and local_path should error at the MCP tier")
require.NotEmpty(t, result.Content)
text, ok := result.Content[0].(mcp.TextContent)
require.True(t, ok)
assert.Contains(t, text.Text, "repository_url",
"MCP-tier error should name the agent-facing alias, not the wire key")
assert.Nil(t, backend.lastPayload, "request must not be forwarded to the backend")
}
func TestMessageTask_ForwardsToBackend(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{
"task_id": "task-target",
"session_id": "sess-1",
"status": "queued",
},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "message_task_kandev", map[string]interface{}{
"task_id": "task-target",
"prompt": "follow up",
})
assert.False(t, result.IsError)
assert.Equal(t, ws.ActionMCPMessageTask, backend.lastAction)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "task-target", payload["task_id"])
assert.Equal(t, "follow up", payload["prompt"])
}
func TestMessageTask_MissingTaskID_ReturnsError(t *testing.T) {
backend := &testBackend{}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "message_task_kandev", map[string]interface{}{
"prompt": "follow up",
})
assert.True(t, result.IsError)
}
func TestMessageTask_MissingPrompt_ReturnsError(t *testing.T) {
backend := &testBackend{}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "message_task_kandev", map[string]interface{}{
"task_id": "task-target",
})
assert.True(t, result.IsError)
}
func TestGetTaskConversation_ForwardsToBackend(t *testing.T) {
backend := &testBackend{
response: map[string]interface{}{
"task_id": "task-target",
"session_id": "sess-1",
"messages": []interface{}{},
"total": 0,
"has_more": false,
"cursor": "",
},
}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "get_task_conversation_kandev", map[string]interface{}{
"task_id": "task-target",
"session_id": "sess-1",
"limit": 25,
"sort": "desc",
"message_types": []interface{}{"message", "tool_call"},
})
assert.False(t, result.IsError)
assert.Equal(t, ws.ActionMCPGetTaskConversation, backend.lastAction)
payload, ok := backend.lastPayload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "task-target", payload["task_id"])
assert.Equal(t, "sess-1", payload["session_id"])
assert.Equal(t, 25, payload["limit"])
assert.Equal(t, "desc", payload["sort"])
assert.Equal(t, []string{"message", "tool_call"}, payload["message_types"])
}
func TestGetTaskConversation_MissingTaskID_ReturnsError(t *testing.T) {
backend := &testBackend{}
s := newTaskModeServer(t, backend, "task-current")
result := callTool(t, s, "get_task_conversation_kandev", map[string]interface{}{})
assert.True(t, result.IsError)
}