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

Function TestMCPHTTP_InitializeRequest

coderd/mcp/mcp_test.go:43–102  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

41}
42
43func TestMCPHTTP_InitializeRequest(t *testing.T) {
44 t.Parallel()
45
46 logger := testutil.Logger(t)
47
48 server, err := mcpserver.NewServer(logger)
49 require.NoError(t, err)
50
51 // Use server directly as http.Handler
52 handler := server
53
54 // Create initialize request
55 initRequest := map[string]any{
56 "jsonrpc": "2.0",
57 "id": 1,
58 "method": "initialize",
59 "params": map[string]any{
60 "protocolVersion": mcp.LATEST_PROTOCOL_VERSION,
61 "capabilities": map[string]any{},
62 "clientInfo": map[string]any{
63 "name": "test-client",
64 "version": "1.0.0",
65 },
66 },
67 }
68
69 body, err := json.Marshal(initRequest)
70 require.NoError(t, err)
71
72 req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body))
73 req.Header.Set("Content-Type", "application/json")
74 req.Header.Set("Accept", "application/json,text/event-stream")
75
76 recorder := httptest.NewRecorder()
77 handler.ServeHTTP(recorder, req)
78
79 if recorder.Code != http.StatusOK {
80 t.Logf("Response body: %s", recorder.Body.String())
81 }
82 assert.Equal(t, http.StatusOK, recorder.Code)
83
84 // Check that a session ID was returned
85 sessionID := recorder.Header().Get("Mcp-Session-Id")
86 assert.NotEmpty(t, sessionID)
87
88 // Parse response
89 var response map[string]any
90 err = json.Unmarshal(recorder.Body.Bytes(), &response)
91 require.NoError(t, err)
92
93 assert.Equal(t, "2.0", response["jsonrpc"])
94 assert.Equal(t, float64(1), response["id"])
95
96 result, ok := response["result"].(map[string]any)
97 require.True(t, ok)
98
99 assert.Equal(t, mcp.LATEST_PROTOCOL_VERSION, result["protocolVersion"])
100 assert.Contains(t, result, "capabilities")

Callers

nothing calls this directly

Calls 13

LoggerFunction · 0.92
NotEmptyMethod · 0.80
SetMethod · 0.65
LogfMethod · 0.65
GetMethod · 0.65
MarshalMethod · 0.45
ServeHTTPMethod · 0.45
StringMethod · 0.45
EqualMethod · 0.45
HeaderMethod · 0.45
UnmarshalMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected