6
6
"encoding/json"
7
7
"errors"
8
8
"fmt"
9
+ "sync"
9
10
"testing"
10
11
"time"
11
12
@@ -1305,6 +1306,7 @@ var globalHookCtx = &globalHookContext{
1305
1306
}
1306
1307
1307
1308
type globalHookContext struct {
1309
+ mu sync.Mutex
1308
1310
BeforeAnyMessages []any
1309
1311
BeforeAnyCount * int
1310
1312
OnSuccessCount * int
@@ -1347,6 +1349,7 @@ func (*globalHook) BeforeAny(ctx context.Context, hookContext HookContext, id an
1347
1349
// Only collect ping messages for our test
1348
1350
if method == mcp .MethodPing {
1349
1351
hookContext .(* globalHookContext ).BeforeAnyMessages = append (hookContext .(* globalHookContext ).BeforeAnyMessages , message )
1352
+ hookContext .(* globalHookContext ).mu .Unlock ()
1350
1353
}
1351
1354
}
1352
1355
@@ -1368,14 +1371,23 @@ func (*globalHook) OnError(ctx context.Context, hookContext HookContext, id any,
1368
1371
}
1369
1372
1370
1373
func (e * globalHook ) BeforeInitialize (ctx context.Context , hookContext HookContext , id any , message * mcp.InitializeRequest ) {
1374
+ hookContext .(* globalHookContext ).mu .Lock ()
1375
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1376
+
1371
1377
e .BeforeAny (ctx , hookContext , id , mcp .MethodInitialize , message )
1372
1378
}
1373
1379
1374
1380
func (e * globalHook ) AfterInitialize (ctx context.Context , hookContext HookContext , id any , message * mcp.InitializeRequest , result * mcp.InitializeResult ) {
1381
+ hookContext .(* globalHookContext ).mu .Lock ()
1382
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1383
+
1375
1384
e .OnSuccess (ctx , hookContext , id , mcp .MethodInitialize , message , result )
1376
1385
}
1377
1386
1378
1387
func (e * globalHook ) BeforePing (ctx context.Context , hookContext HookContext , id any , message * mcp.PingRequest ) {
1388
+ hookContext .(* globalHookContext ).mu .Lock ()
1389
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1390
+
1379
1391
e .BeforeAny (ctx , hookContext , id , mcp .MethodPing , message )
1380
1392
1381
1393
newCount := (* hookContext .(* globalHookContext ).BeforePingCount ) + 1
@@ -1385,6 +1397,9 @@ func (e *globalHook) BeforePing(ctx context.Context, hookContext HookContext, id
1385
1397
}
1386
1398
1387
1399
func (e * globalHook ) AfterPing (ctx context.Context , hookContext HookContext , id any , message * mcp.PingRequest , result * mcp.EmptyResult ) {
1400
+ hookContext .(* globalHookContext ).mu .Lock ()
1401
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1402
+
1388
1403
e .OnSuccess (ctx , hookContext , id , mcp .MethodPing , message , result )
1389
1404
1390
1405
newCount := (* hookContext .(* globalHookContext ).AfterPingCount ) + 1
@@ -1427,24 +1442,36 @@ func (*globalHook) AfterGetPrompt(ctx context.Context, hookContext HookContext,
1427
1442
}
1428
1443
1429
1444
func (e * globalHook ) BeforeListTools (ctx context.Context , hookContext HookContext , id any , message * mcp.ListToolsRequest ) {
1445
+ hookContext .(* globalHookContext ).mu .Lock ()
1446
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1447
+
1430
1448
e .BeforeAny (ctx , hookContext , id , mcp .MethodToolsList , message )
1431
1449
1432
1450
newCount := (* hookContext .(* globalHookContext ).BeforeToolsCount ) + 1
1433
1451
hookContext .(* globalHookContext ).BeforeToolsCount = & (newCount )
1434
1452
}
1435
1453
1436
1454
func (e * globalHook ) AfterListTools (ctx context.Context , hookContext HookContext , id any , message * mcp.ListToolsRequest , result * mcp.ListToolsResult ) {
1455
+ hookContext .(* globalHookContext ).mu .Lock ()
1456
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1457
+
1437
1458
e .OnSuccess (ctx , hookContext , id , mcp .MethodToolsList , message , result )
1438
1459
1439
1460
newCount := (* hookContext .(* globalHookContext ).AfterToolsCount ) + 1
1440
1461
hookContext .(* globalHookContext ).AfterToolsCount = & (newCount )
1441
1462
}
1442
1463
1443
1464
func (e * globalHook ) BeforeCallTool (ctx context.Context , hookContext HookContext , id any , message * mcp.CallToolRequest ) {
1465
+ hookContext .(* globalHookContext ).mu .Lock ()
1466
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1467
+
1444
1468
e .BeforeAny (ctx , hookContext , id , mcp .MethodToolsCall , message )
1445
1469
}
1446
1470
1447
1471
func (e * globalHook ) AfterCallTool (ctx context.Context , hookContext HookContext , id any , message * mcp.CallToolRequest , result * mcp.CallToolResult ) {
1472
+ hookContext .(* globalHookContext ).mu .Lock ()
1473
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1474
+
1448
1475
e .OnSuccess (ctx , hookContext , id , mcp .MethodToolsCall , message , result )
1449
1476
}
1450
1477
0 commit comments