@@ -59,6 +59,7 @@ func TestMCPServer_Capabilities(t *testing.T) {
59
59
WithPromptCapabilities (true ),
60
60
WithToolCapabilities (true ),
61
61
WithLogging (),
62
+ WithCompletions (),
62
63
},
63
64
validate : func (t * testing.T , response mcp.JSONRPCMessage ) {
64
65
resp , ok := response .(mcp.JSONRPCResponse )
@@ -87,6 +88,7 @@ func TestMCPServer_Capabilities(t *testing.T) {
87
88
assert .True (t , initResult .Capabilities .Tools .ListChanged )
88
89
89
90
assert .NotNil (t , initResult .Capabilities .Logging )
91
+ assert .NotNil (t , initResult .Capabilities .Completion )
90
92
},
91
93
},
92
94
{
@@ -1458,6 +1460,119 @@ func TestMCPServer_ResourceTemplates(t *testing.T) {
1458
1460
})
1459
1461
}
1460
1462
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
+
1461
1576
func createTestServer () * MCPServer {
1462
1577
server := NewMCPServer ("test-server" , "1.0.0" ,
1463
1578
WithResourceCapabilities (true , true ),
0 commit comments