TestExpMcpServerOptionalUserToken checks that the MCP server works with just an agent socket and no user token, with certain tools available (like coder_report_task).
(t *testing.T)
| 562 | // an agent socket and no user token, with certain tools available (like |
| 563 | // coder_report_task). |
| 564 | func TestExpMcpServerOptionalUserToken(t *testing.T) { |
| 565 | t.Parallel() |
| 566 | |
| 567 | // Reading to / writing from the PTY is flaky on non-linux systems. |
| 568 | if runtime.GOOS != "linux" { |
| 569 | t.Skip("skipping on non-linux") |
| 570 | } |
| 571 | |
| 572 | ctx := testutil.Context(t, testutil.WaitMedium) |
| 573 | cmdDone := make(chan struct{}) |
| 574 | cancelCtx, cancel := context.WithCancel(ctx) |
| 575 | t.Cleanup(cancel) |
| 576 | |
| 577 | // Create a test deployment with a workspace and agent. |
| 578 | client, db := coderdtest.NewWithDatabase(t, nil) |
| 579 | user := coderdtest.CreateFirstUser(t, client) |
| 580 | r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ |
| 581 | OrganizationID: user.OrganizationID, |
| 582 | OwnerID: user.UserID, |
| 583 | }).WithAgent(func(a []*proto.Agent) []*proto.Agent { |
| 584 | a[0].Apps = []*proto.App{{Slug: "test-app"}} |
| 585 | return a |
| 586 | }).Do() |
| 587 | |
| 588 | // Start a real agent with the socket server enabled. |
| 589 | socketPath := testutil.AgentSocketPath(t) |
| 590 | _ = agenttest.New(t, client.URL, r.AgentToken, func(o *agent.Options) { |
| 591 | o.SocketServerEnabled = true |
| 592 | o.SocketPath = socketPath |
| 593 | }) |
| 594 | coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID) |
| 595 | |
| 596 | inv, _ := clitest.New(t, |
| 597 | "exp", "mcp", "server", |
| 598 | "--socket-path", socketPath, |
| 599 | "--app-status-slug", "test-app", |
| 600 | ) |
| 601 | inv = inv.WithContext(cancelCtx) |
| 602 | |
| 603 | pty := ptytest.New(t) |
| 604 | inv.Stdin = pty.Input() |
| 605 | inv.Stdout = pty.Output() |
| 606 | |
| 607 | go func() { |
| 608 | defer close(cmdDone) |
| 609 | err := inv.Run() |
| 610 | assert.NoError(t, err) |
| 611 | }() |
| 612 | |
| 613 | // Verify server starts by checking for a successful initialization |
| 614 | payload := `{"jsonrpc":"2.0","id":1,"method":"initialize"}` |
| 615 | pty.WriteLine(payload) |
| 616 | _ = pty.ReadLine(ctx) // ignore echoed output |
| 617 | output := pty.ReadLine(ctx) |
| 618 | |
| 619 | // Ensure we get a valid response |
| 620 | var initializeResponse map[string]interface{} |
| 621 | err := json.Unmarshal([]byte(output), &initializeResponse) |
nothing calls this directly
no test coverage detected