Skip to content

Commit bd463ef

Browse files
authored
fix(mcp): MCPServer#invalidateToolsCache() not exposed while being mentioned in the documents . Fix #219 (#240)
* Fix #219 MCPServer#invalidateToolsCache() not exposed while being mentioned in the documents * Add changeset
1 parent 18ceabc commit bd463ef

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

.changeset/open-candies-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/agents-core': patch
3+
---
4+
5+
Fix #219 MCPServer#invalidateToolsCache() not exposed while being mentioned in the documents

packages/agents-core/src/mcp.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface MCPServer {
3535
toolName: string,
3636
args: Record<string, unknown> | null,
3737
): Promise<CallToolResultContent>;
38+
invalidateToolsCache(): Promise<void>;
3839
}
3940

4041
export abstract class BaseMCPServerStdio implements MCPServer {
@@ -56,6 +57,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
5657
_toolName: string,
5758
_args: Record<string, unknown> | null,
5859
): Promise<CallToolResultContent>;
60+
abstract invalidateToolsCache(): Promise<void>;
5961

6062
/**
6163
* Logs a debug message when debug logging is enabled.
@@ -89,6 +91,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
8991
_toolName: string,
9092
_args: Record<string, unknown> | null,
9193
): Promise<CallToolResultContent>;
94+
abstract invalidateToolsCache(): Promise<void>;
9295

9396
/**
9497
* Logs a debug message when debug logging is enabled.
@@ -154,6 +157,9 @@ export class MCPServerStdio extends BaseMCPServerStdio {
154157
): Promise<CallToolResultContent> {
155158
return this.underlying.callTool(toolName, args);
156159
}
160+
invalidateToolsCache(): Promise<void> {
161+
return this.underlying.invalidateToolsCache();
162+
}
157163
}
158164

159165
export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
@@ -187,6 +193,9 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
187193
): Promise<CallToolResultContent> {
188194
return this.underlying.callTool(toolName, args);
189195
}
196+
invalidateToolsCache(): Promise<void> {
197+
return this.underlying.invalidateToolsCache();
198+
}
190199
}
191200

192201
/**
@@ -225,7 +234,7 @@ const _cachedTools: Record<string, MCPTool[]> = {};
225234
*
226235
* @param serverName - Name of the MCP server whose cache should be cleared.
227236
*/
228-
export function invalidateServerToolsCache(serverName: string) {
237+
export async function invalidateServerToolsCache(serverName: string) {
229238
delete _cachedTools[serverName];
230239
}
231240
/**

packages/agents-core/src/shims/mcp-server/browser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export class MCPServerStdio extends BaseMCPServerStdio {
2929
): Promise<CallToolResultContent> {
3030
throw new Error('Method not implemented.');
3131
}
32+
invalidateToolsCache(): Promise<void> {
33+
throw new Error('Method not implemented.');
34+
}
3235
}
3336

3437
export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
@@ -53,4 +56,7 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
5356
): Promise<CallToolResultContent> {
5457
throw new Error('Method not implemented.');
5558
}
59+
invalidateToolsCache(): Promise<void> {
60+
throw new Error('Method not implemented.');
61+
}
5662
}

packages/agents-core/src/shims/mcp-server/node.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ export class NodeMCPServerStdio extends BaseMCPServerStdio {
9191
this.debugLog(() => `Connected to MCP server: ${this._name}`);
9292
}
9393

94-
invalidateToolsCache() {
95-
invalidateServerToolsCache(this.name);
94+
async invalidateToolsCache(): Promise<void> {
95+
await invalidateServerToolsCache(this.name);
9696
this._cacheDirty = true;
9797
}
9898

@@ -208,8 +208,8 @@ export class NodeMCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
208208
this.debugLog(() => `Connected to MCP server: ${this._name}`);
209209
}
210210

211-
invalidateToolsCache() {
212-
invalidateServerToolsCache(this.name);
211+
async invalidateToolsCache(): Promise<void> {
212+
await invalidateServerToolsCache(this.name);
213213
this._cacheDirty = true;
214214
}
215215

packages/agents-core/test/mcpCache.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('MCP tools cache invalidation', () => {
5656
tools = await getAllMcpTools([server]);
5757
expect(tools.map((t) => t.name)).toEqual(['a']);
5858

59-
server.invalidateToolsCache();
59+
await server.invalidateToolsCache();
6060
tools = await getAllMcpTools([server]);
6161
expect(tools.map((t) => t.name)).toEqual(['b']);
6262
});

0 commit comments

Comments
 (0)