Skip to content

Commit c16e99f

Browse files
test: test prompt arg completion
1 parent bf34fb2 commit c16e99f

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

server/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ func WithLogging() ServerOption {
282282
}
283283
}
284284

285+
// WithCompletions enables autocomplete capabilities for the server
286+
func WithCompletions() ServerOption {
287+
return func(s *MCPServer) {
288+
s.capabilities.completions = mcp.ToBoolPtr(true)
289+
}
290+
}
291+
285292
// WithInstructions sets the server instructions for the client returned in the initialize response
286293
func WithInstructions(instructions string) ServerOption {
287294
return func(s *MCPServer) {

server/server_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func TestMCPServer_Capabilities(t *testing.T) {
5959
WithPromptCapabilities(true),
6060
WithToolCapabilities(true),
6161
WithLogging(),
62+
WithCompletions(),
6263
},
6364
validate: func(t *testing.T, response mcp.JSONRPCMessage) {
6465
resp, ok := response.(mcp.JSONRPCResponse)
@@ -87,6 +88,7 @@ func TestMCPServer_Capabilities(t *testing.T) {
8788
assert.True(t, initResult.Capabilities.Tools.ListChanged)
8889

8990
assert.NotNil(t, initResult.Capabilities.Logging)
91+
assert.NotNil(t, initResult.Capabilities.Completion)
9092
},
9193
},
9294
{
@@ -1458,6 +1460,119 @@ func TestMCPServer_ResourceTemplates(t *testing.T) {
14581460
})
14591461
}
14601462

1463+
func TestMCPServer_HandleCompletion(t *testing.T) {
1464+
server := NewMCPServer("test-server", "1.0.0")
1465+
1466+
server.AddPrompt(
1467+
mcp.NewPrompt("test-prompt",
1468+
mcp.WithArgument("test-arg", mcp.ArgumentCompletion(func(_ context.Context, req mcp.CompleteRequest) (*mcp.CompleteResult, error) {
1469+
return &mcp.CompleteResult{
1470+
Completion: mcp.Completion{
1471+
Values: []string{fmt.Sprintf("%sbar", req.Params.Argument.Value)},
1472+
},
1473+
}, nil
1474+
})),
1475+
),
1476+
func(ctx context.Context, req mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
1477+
return nil, nil
1478+
},
1479+
)
1480+
1481+
tests := []struct {
1482+
name string
1483+
message string
1484+
validate func(t *testing.T, response mcp.JSONRPCMessage)
1485+
}{
1486+
{
1487+
name: "Prompt argument completion",
1488+
message: `{
1489+
"jsonrpc": "2.0",
1490+
"id": 1,
1491+
"method": "completion/complete",
1492+
"params": {
1493+
"ref": {
1494+
"type": "ref/prompt",
1495+
"name": "test-prompt"
1496+
},
1497+
"argument": {
1498+
"name": "test-arg",
1499+
"value": "foo"
1500+
}
1501+
}
1502+
}`,
1503+
validate: func(t *testing.T, response mcp.JSONRPCMessage) {
1504+
resp, ok := response.(mcp.JSONRPCResponse)
1505+
assert.True(t, ok)
1506+
1507+
result, ok := resp.Result.(mcp.CompleteResult)
1508+
assert.True(t, ok)
1509+
1510+
assert.Len(t, result.Completion.Values, 1)
1511+
assert.Equal(t, "foobar", result.Completion.Values[0])
1512+
},
1513+
},
1514+
{
1515+
name: "No completion for prompt argument",
1516+
message: `{
1517+
"jsonrpc": "2.0",
1518+
"id": 1,
1519+
"method": "completion/complete",
1520+
"params": {
1521+
"ref": {
1522+
"type": "ref/prompt",
1523+
"name": "test-prompt"
1524+
},
1525+
"argument": {
1526+
"name": "another-arg",
1527+
"value": "foo"
1528+
}
1529+
}
1530+
}`,
1531+
validate: func(t *testing.T, response mcp.JSONRPCMessage) {
1532+
resp, ok := response.(mcp.JSONRPCResponse)
1533+
assert.True(t, ok)
1534+
1535+
result, ok := resp.Result.(mcp.CompleteResult)
1536+
assert.True(t, ok)
1537+
1538+
assert.NotNil(t, result.Completion.Values)
1539+
assert.Len(t, result.Completion.Values, 0)
1540+
},
1541+
},
1542+
{
1543+
name: "Prompt not found",
1544+
message: `{
1545+
"jsonrpc": "2.0",
1546+
"id": 1,
1547+
"method": "completion/complete",
1548+
"params": {
1549+
"ref": {
1550+
"type": "ref/prompt",
1551+
"name": "unknown-prompt"
1552+
},
1553+
"argument": {
1554+
"name": "test-arg",
1555+
"value": "foo"
1556+
}
1557+
}
1558+
}`,
1559+
validate: func(t *testing.T, response mcp.JSONRPCMessage) {
1560+
resp, ok := response.(mcp.JSONRPCError)
1561+
assert.True(t, ok)
1562+
assert.Equal(t, resp.Error.Code, mcp.INVALID_PARAMS)
1563+
},
1564+
},
1565+
}
1566+
1567+
for _, tt := range tests {
1568+
t.Run(tt.name, func(t *testing.T) {
1569+
response := server.HandleMessage(context.Background(), []byte(tt.message))
1570+
assert.NotNil(t, response)
1571+
tt.validate(t, response)
1572+
})
1573+
}
1574+
}
1575+
14611576
func createTestServer() *MCPServer {
14621577
server := NewMCPServer("test-server", "1.0.0",
14631578
WithResourceCapabilities(true, true),

0 commit comments

Comments
 (0)