(t *testing.T)
| 37 | ) |
| 38 | |
| 39 | func TestExpMcpServer(t *testing.T) { |
| 40 | t.Parallel() |
| 41 | |
| 42 | // Reading to / writing from the PTY is flaky on non-linux systems. |
| 43 | if runtime.GOOS != "linux" { |
| 44 | t.Skip("skipping on non-linux") |
| 45 | } |
| 46 | |
| 47 | t.Run("AllowedTools", func(t *testing.T) { |
| 48 | t.Parallel() |
| 49 | |
| 50 | ctx := testutil.Context(t, testutil.WaitShort) |
| 51 | cmdDone := make(chan struct{}) |
| 52 | cancelCtx, cancel := context.WithCancel(ctx) |
| 53 | |
| 54 | // Given: a running coder deployment |
| 55 | client := coderdtest.New(t, nil) |
| 56 | owner := coderdtest.CreateFirstUser(t, client) |
| 57 | |
| 58 | // Given: we run the exp mcp command with allowed tools set |
| 59 | inv, root := clitest.New(t, "exp", "mcp", "server", "--allowed-tools=coder_get_authenticated_user") |
| 60 | inv = inv.WithContext(cancelCtx) |
| 61 | |
| 62 | pty := ptytest.New(t) |
| 63 | inv.Stdin = pty.Input() |
| 64 | inv.Stdout = pty.Output() |
| 65 | // nolint: gocritic // not the focus of this test |
| 66 | clitest.SetupConfig(t, client, root) |
| 67 | |
| 68 | go func() { |
| 69 | defer close(cmdDone) |
| 70 | err := inv.Run() |
| 71 | assert.NoError(t, err) |
| 72 | }() |
| 73 | |
| 74 | // When: we send a tools/list request |
| 75 | toolsPayload := `{"jsonrpc":"2.0","id":2,"method":"tools/list"}` |
| 76 | pty.WriteLine(toolsPayload) |
| 77 | _ = pty.ReadLine(ctx) // ignore echoed output |
| 78 | output := pty.ReadLine(ctx) |
| 79 | |
| 80 | // Then: we should only see the allowed tools in the response |
| 81 | var toolsResponse struct { |
| 82 | Result struct { |
| 83 | Tools []struct { |
| 84 | Name string `json:"name"` |
| 85 | Annotations struct { |
| 86 | ReadOnlyHint *bool `json:"readOnlyHint"` |
| 87 | DestructiveHint *bool `json:"destructiveHint"` |
| 88 | IdempotentHint *bool `json:"idempotentHint"` |
| 89 | OpenWorldHint *bool `json:"openWorldHint"` |
| 90 | } `json:"annotations"` |
| 91 | } `json:"tools"` |
| 92 | } `json:"result"` |
| 93 | } |
| 94 | err := json.Unmarshal([]byte(output), &toolsResponse) |
| 95 | require.NoError(t, err) |
| 96 | require.Len(t, toolsResponse.Result.Tools, 1, "should have exactly 1 tool") |
nothing calls this directly
no test coverage detected