MCPcopy Index your code
hub / github.com/coder/coder / TestMCPHTTP_E2E_ToolWithWorkspace

Function TestMCPHTTP_E2E_ToolWithWorkspace

coderd/mcp/mcp_e2e_test.go:223–295  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

221}
222
223func TestMCPHTTP_E2E_ToolWithWorkspace(t *testing.T) {
224 t.Parallel()
225
226 coderClient, closer, api := coderdtest.NewWithAPI(t, nil)
227 defer closer.Close()
228
229 user := coderdtest.CreateFirstUser(t, coderClient)
230 r := dbfake.WorkspaceBuild(t, api.Database, database.WorkspaceTable{
231 Name: "myworkspace",
232 OrganizationID: user.OrganizationID,
233 OwnerID: user.UserID,
234 }).WithAgent().Do()
235
236 fs := afero.NewMemMapFs()
237 tmpdir := os.TempDir()
238 require.NoError(t, fs.MkdirAll(tmpdir, 0o755))
239 filePath := filepath.Join(tmpdir, "mcp-http-test.txt")
240 require.NoError(t, afero.WriteFile(fs, filePath, []byte("hello from mcp"), 0o644))
241
242 _ = agenttest.New(t, coderClient.URL, r.AgentToken, func(opts *agent.Options) {
243 opts.Filesystem = fs
244 })
245 coderdtest.NewWorkspaceAgentWaiter(t, coderClient, r.Workspace.ID).Wait()
246
247 mcpURL := api.AccessURL.String() + mcpserver.MCPEndpoint
248 mcpClient, err := mcpclient.NewStreamableHttpClient(mcpURL,
249 transport.WithHTTPHeaders(map[string]string{
250 "Authorization": "Bearer " + coderClient.SessionToken(),
251 }))
252 require.NoError(t, err)
253 defer func() {
254 if closeErr := mcpClient.Close(); closeErr != nil {
255 t.Logf("Failed to close MCP client: %v", closeErr)
256 }
257 }()
258
259 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
260 defer cancel()
261
262 require.NoError(t, mcpClient.Start(ctx))
263 _, err = mcpClient.Initialize(ctx, mcp.InitializeRequest{
264 Params: mcp.InitializeParams{
265 ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION,
266 ClientInfo: mcp.Implementation{
267 Name: "test-client-workspace",
268 Version: "1.0.0",
269 },
270 },
271 })
272 require.NoError(t, err)
273
274 toolResult, err := mcpClient.CallTool(ctx, mcp.CallToolRequest{
275 Params: mcp.CallToolParams{
276 Name: toolsdk.ToolNameWorkspaceLS,
277 Arguments: map[string]any{
278 "workspace": r.Workspace.Name,
279 "path": tmpdir,
280 },

Callers

nothing calls this directly

Calls 15

NewWithAPIFunction · 0.92
CreateFirstUserFunction · 0.92
WorkspaceBuildFunction · 0.92
NewFunction · 0.92
NewWorkspaceAgentWaiterFunction · 0.92
WithAgentMethod · 0.80
MkdirAllMethod · 0.80
InitializeMethod · 0.80
NotEmptyMethod · 0.80
CloseMethod · 0.65
DoMethod · 0.65
TempDirMethod · 0.65

Tested by

no test coverage detected