1+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
12import * as stdioModule from "@modelcontextprotocol/sdk/server/stdio.js" ;
23import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
34import createServer , {
@@ -22,6 +23,9 @@ const testDoubles = vi.hoisted(() => ({
2223 end : vi . fn ( ) ,
2324 } ,
2425 connect : vi . fn ( ) . mockResolvedValue ( undefined ) ,
26+ tool : vi . fn ( ) ,
27+ registerTool : vi . fn ( ) ,
28+ directRegisterToolCalls : 0 ,
2529 sentry : {
2630 init : vi . fn ( ( ) => ( { } ) ) ,
2731 setUser : vi . fn ( ) ,
@@ -68,6 +72,13 @@ vi.mock("./utils/metrics.js", () => ({
6872 serverStartups : { add : vi . fn ( ) } ,
6973} ) ) ;
7074
75+ vi . mock ( "./tools/user.js" , ( ) => ( {
76+ registerUserTools : vi . fn ( ( server : McpServer ) => {
77+ testDoubles . directRegisterToolCalls += 1 ;
78+ server . registerTool ( "get-user-info" , { } , vi . fn ( ) ) ;
79+ } ) ,
80+ } ) ) ;
81+
7182vi . mock ( "@opentelemetry/api" , ( ) => ( {
7283 SpanStatusCode : { OK : 1 , ERROR : 2 } ,
7384 trace : { getTracer : vi . fn ( ( ) => ( { startActiveSpan : vi . fn ( ) } ) ) } ,
@@ -82,8 +93,8 @@ vi.mock("@opentelemetry/api", () => ({
8293vi . mock ( "@modelcontextprotocol/sdk/server/mcp.js" , ( ) => {
8394 class MockMcpServer {
8495 connect = testDoubles . connect ;
85- registerTool = vi . fn ( ) ;
86- tool = vi . fn ( ) ;
96+ tool = testDoubles . tool ;
97+ registerTool = testDoubles . registerTool ;
8798 registerResource = vi . fn ( ) ;
8899 }
89100
@@ -111,6 +122,12 @@ describe("Server entry", () => {
111122 process . env = { ...originalEnv } ;
112123 process . argv = [ ...originalArgv ] ;
113124 vi . clearAllMocks ( ) ;
125+ testDoubles . directRegisterToolCalls = 0 ;
126+ testDoubles . tool . mockImplementation (
127+ function ( this : { registerTool : ( ) => void } ) {
128+ this . registerTool ( ) ;
129+ } ,
130+ ) ;
114131 const anyStdioModule = stdioModule as { __transports ?: unknown [ ] } ;
115132 if ( anyStdioModule . __transports ) {
116133 anyStdioModule . __transports . length = 0 ;
@@ -142,6 +159,17 @@ describe("Server entry", () => {
142159 ) ;
143160 } ) ;
144161
162+ it ( "reports the number of tool registration calls on the registration span" , ( ) => {
163+ createServer ( { config : { apiKey : "test-key" } } ) ;
164+
165+ const registrationCount = testDoubles . registerTool . mock . calls . length ;
166+ expect ( registrationCount ) . toBeGreaterThan ( 0 ) ;
167+ expect ( testDoubles . span . setAttribute ) . toHaveBeenCalledWith (
168+ "mcp.tools.count" ,
169+ registrationCount ,
170+ ) ;
171+ } ) ;
172+
145173 it ( "exports createServer as both default and named exports" , ( ) => {
146174 expect ( namedCreateServer ) . toBe ( createServer ) ;
147175 const server = namedCreateServer ( { config : { apiKey : "named-key" } } ) ;
0 commit comments