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
@@ -1368,14 +1370,23 @@ func (*globalHook) OnError(ctx context.Context, hookContext HookContext, id any,
1368
1370
}
1369
1371
1370
1372
func (e * globalHook ) BeforeInitialize (ctx context.Context , hookContext HookContext , id any , message * mcp.InitializeRequest ) {
1373
+ hookContext .(* globalHookContext ).mu .Lock ()
1374
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1375
+
1371
1376
e .BeforeAny (ctx , hookContext , id , mcp .MethodInitialize , message )
1372
1377
}
1373
1378
1374
1379
func (e * globalHook ) AfterInitialize (ctx context.Context , hookContext HookContext , id any , message * mcp.InitializeRequest , result * mcp.InitializeResult ) {
1380
+ hookContext .(* globalHookContext ).mu .Lock ()
1381
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1382
+
1375
1383
e .OnSuccess (ctx , hookContext , id , mcp .MethodInitialize , message , result )
1376
1384
}
1377
1385
1378
1386
func (e * globalHook ) BeforePing (ctx context.Context , hookContext HookContext , id any , message * mcp.PingRequest ) {
1387
+ hookContext .(* globalHookContext ).mu .Lock ()
1388
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1389
+
1379
1390
e .BeforeAny (ctx , hookContext , id , mcp .MethodPing , message )
1380
1391
1381
1392
newCount := (* hookContext .(* globalHookContext ).BeforePingCount ) + 1
@@ -1385,6 +1396,9 @@ func (e *globalHook) BeforePing(ctx context.Context, hookContext HookContext, id
1385
1396
}
1386
1397
1387
1398
func (e * globalHook ) AfterPing (ctx context.Context , hookContext HookContext , id any , message * mcp.PingRequest , result * mcp.EmptyResult ) {
1399
+ hookContext .(* globalHookContext ).mu .Lock ()
1400
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1401
+
1388
1402
e .OnSuccess (ctx , hookContext , id , mcp .MethodPing , message , result )
1389
1403
1390
1404
newCount := (* hookContext .(* globalHookContext ).AfterPingCount ) + 1
@@ -1427,24 +1441,36 @@ func (*globalHook) AfterGetPrompt(ctx context.Context, hookContext HookContext,
1427
1441
}
1428
1442
1429
1443
func (e * globalHook ) BeforeListTools (ctx context.Context , hookContext HookContext , id any , message * mcp.ListToolsRequest ) {
1444
+ hookContext .(* globalHookContext ).mu .Lock ()
1445
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1446
+
1430
1447
e .BeforeAny (ctx , hookContext , id , mcp .MethodToolsList , message )
1431
1448
1432
1449
newCount := (* hookContext .(* globalHookContext ).BeforeToolsCount ) + 1
1433
1450
hookContext .(* globalHookContext ).BeforeToolsCount = & (newCount )
1434
1451
}
1435
1452
1436
1453
func (e * globalHook ) AfterListTools (ctx context.Context , hookContext HookContext , id any , message * mcp.ListToolsRequest , result * mcp.ListToolsResult ) {
1454
+ hookContext .(* globalHookContext ).mu .Lock ()
1455
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1456
+
1437
1457
e .OnSuccess (ctx , hookContext , id , mcp .MethodToolsList , message , result )
1438
1458
1439
1459
newCount := (* hookContext .(* globalHookContext ).AfterToolsCount ) + 1
1440
1460
hookContext .(* globalHookContext ).AfterToolsCount = & (newCount )
1441
1461
}
1442
1462
1443
1463
func (e * globalHook ) BeforeCallTool (ctx context.Context , hookContext HookContext , id any , message * mcp.CallToolRequest ) {
1464
+ hookContext .(* globalHookContext ).mu .Lock ()
1465
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1466
+
1444
1467
e .BeforeAny (ctx , hookContext , id , mcp .MethodToolsCall , message )
1445
1468
}
1446
1469
1447
1470
func (e * globalHook ) AfterCallTool (ctx context.Context , hookContext HookContext , id any , message * mcp.CallToolRequest , result * mcp.CallToolResult ) {
1471
+ hookContext .(* globalHookContext ).mu .Lock ()
1472
+ defer hookContext .(* globalHookContext ).mu .Unlock ()
1473
+
1448
1474
e .OnSuccess (ctx , hookContext , id , mcp .MethodToolsCall , message , result )
1449
1475
}
1450
1476
0 commit comments