Skip to content

[Bug] openai-agents (TypeScript): invalidateToolsCache() on a plugin MCP server keeps the old tool list #241

Open

Description

@campoy

What are you really trying to do?

Refresh an MCP server's tool list inside a Workflow after the server's catalog changes. The
OpenAI Agents SDK documents calling
invalidateToolsCache() on the server for this when cacheToolsList is on, and
statelessMcpServer() and statefulMcpServer() return servers with that method and with
cacheToolsList: true by default.

Describe the bug

After invalidateToolsCache(), the next agent run in the same Workflow still gets the old tool
names and schemas. No list-tools Activity runs, and nothing warns.

The agent SDK keeps a shared tool cache keyed by server name (cachedMcpTools). Native MCP
servers clear it in invalidateToolsCache() by calling invalidateServerToolsCache(this.name),
which @openai/agents-core exports. The plugin's references don't:

  • statelessMcpServer() clears only its own cachedTools.
  • StatefulMCPServerReference.invalidateToolsCache() does nothing. It also never reaches the
    Worker-side server, so a factory server with cacheToolsList: true keeps its own list even when
    the list-tools Activity runs again.

A fresh Workflow, or a reference with cacheToolsList: false, gets the new list.

Minimal Reproduction

One file. The model and the MCP servers are fakes; the Worker, the plugin and the agent runner
are real. The servers list old_tool first and new_tool after that. The stateful one keeps its
own list until invalidated, like a native server with cacheToolsList: true. Run it against a dev
server (temporal server start-dev) with @temporalio/openai-agents, @temporalio/client,
@temporalio/worker, @temporalio/workflow, @openai/agents-core and zod installed:

// node issue_repro.js <temporal-address>
// Exits 1 if invalidateToolsCache() on a plugin MCP reference leaves the next run with the old
// tool list, 0 if the next run gets the new one. Only the model and the MCP server are fakes.
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { Client, Connection } = require('@temporalio/client');
const { DefaultLogger, NativeConnection, Runtime, Worker, bundleWorkflowCode } = require('@temporalio/worker');
const { OpenAIAgentsPlugin, StatefulMCPServerProvider, StatelessMCPServerProvider } = require('@temporalio/openai-agents');
const { Usage } = require('@openai/agents-core');

Runtime.install({ logger: new DefaultLogger('WARN') });

const workflowSource = `
const { Agent } = require('@openai/agents-core');
const { TemporalOpenAIRunner, statefulMcpServer, statelessMcpServer } = require('@temporalio/openai-agents/workflow');
exports.catalogWorkflow = async (stateful) => {
  const server = stateful ? statefulMcpServer('catalog') : statelessMcpServer('catalog');
  await server.connect();
  try {
    const agent = new Agent({ name: 'catalog', model: 'fake', mcpServers: [server] });
    const runner = new TemporalOpenAIRunner();
    const first = await runner.run(agent, 'Which tools do you have?');
    await server.invalidateToolsCache();
    const second = await runner.run(agent, 'Which tools do you have now?');
    return [JSON.parse(first.finalOutput), JSON.parse(second.finalOutput)];
  } finally {
    await server.close();
  }
};`;

const tool = (name) => ({ name, description: name, inputSchema: { type: 'object', properties: {}, required: [] } });

// The server's catalog changes after the first listing: old_tool, then new_tool.
let statelessLists = 0;
const statelessServer = () => ({
  name: 'catalog',
  cacheToolsList: false,
  async connect() {},
  async close() {},
  async invalidateToolsCache() {},
  async listTools() {
    statelessLists++;
    return [tool(statelessLists === 1 ? 'old_tool' : 'new_tool')];
  },
  async callTool() {
    throw new Error('not used');
  },
});

// Like a native server with cacheToolsList: true, this one keeps its own list until invalidated.
let statefulLists = 0;
const statefulServer = () => {
  let cached;
  return {
    name: 'catalog',
    cacheToolsList: true,
    async connect() {},
    async close() {},
    async invalidateToolsCache() {
      cached = undefined;
    },
    async listTools() {
      if (!cached) {
        statefulLists++;
        cached = [tool(statefulLists === 1 ? 'old_tool' : 'new_tool')];
      }
      return cached;
    },
    async callTool() {
      throw new Error('not used');
    },
  };
};

// The model answers with the names of the tools it was offered.
const model = {
  async getResponse(request) {
    const text = JSON.stringify(request.tools.map((t) => t.name));
    return { usage: new Usage(), output: [{ type: 'message', id: 'm', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text }] }] };
  },
  async *getStreamedResponse() {
    throw new Error('not used');
  },
};

(async () => {
  const address = process.argv[2] ?? '127.0.0.1:7233';
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-invalidate-'));
  const workflowsPath = path.join(dir, 'workflows.js');
  fs.writeFileSync(workflowsPath, workflowSource);
  const nativeConnection = await NativeConnection.connect({ address });
  const plugin = new OpenAIAgentsPlugin({
    modelProvider: { getModel: () => model },
    mcpServerProviders: [
      new StatelessMCPServerProvider('catalog', statelessServer),
      new StatefulMCPServerProvider('catalog', statefulServer, nativeConnection),
    ],
  });
  const workflowBundle = await bundleWorkflowCode({
    workflowsPath,
    plugins: [plugin],
    logger: new DefaultLogger('WARN'),
    webpackConfigHook: (config) => ({ ...config, resolve: { ...config.resolve, modules: [path.join(process.cwd(), 'node_modules'), 'node_modules'] } }),
  });
  const taskQueue = `mcp-invalidate-${Date.now()}`;
  const worker = await Worker.create({ connection: nativeConnection, taskQueue, workflowBundle, plugins: [plugin] });
  const client = new Client({ connection: await Connection.connect({ address }), plugins: [plugin] });
  let stale = false;
  await worker.runUntil(async () => {
    for (const stateful of [false, true]) {
      const [first, second] = await client.workflow.execute('catalogWorkflow', {
        taskQueue,
        workflowId: `${taskQueue}-${stateful ? 'stateful' : 'stateless'}`,
        args: [stateful],
      });
      const kind = stateful ? 'statefulMcpServer' : 'statelessMcpServer';
      console.log(`${kind}: before invalidateToolsCache ${JSON.stringify(first)}, after ${JSON.stringify(second)}`);
      if (second[0] !== 'new_tool') stale = true;
    }
  });
  await nativeConnection.close();
  console.log(stale ? 'BUG: the run after invalidateToolsCache() still got the old tool list' : 'OK: the run after invalidateToolsCache() got the new tool list');
  process.exit(stale ? 1 : 0);
})().catch((err) => {
  console.error(err);
  process.exit(2);
});

Output with npm @temporalio/openai-agents 1.24.0 and with ai-integrations main at c664a34 (Worker log lines removed):

statelessMcpServer: before invalidateToolsCache ["old_tool"], after ["old_tool"]
statefulMcpServer: before invalidateToolsCache ["old_tool"], after ["old_tool"]
BUG: the run after invalidateToolsCache() still got the old tool list

The script exits 1 when the run after invalidateToolsCache() gets the old list, and 0 when it
gets the new one.

Expected behavior

As with a native MCP server: after invalidateToolsCache(), the next run lists tools again and the
model gets new_tool.

Impact

The model keeps getting tool names and parameter schemas the server no longer lists, and the run
gives no error.

Proposed fix

In both references' invalidateToolsCache(), also call invalidateServerToolsCache(name) from
@openai/agents-core, as native servers do. For a stateful reference, have the next list-tools
Activity call invalidateToolsCache() on the Worker-side server before it lists.

The next run then schedules a list-tools Activity that it skips today. Open Workflows that
invalidated and then ran the agent again would replay differently, so the change needs a
patched check. A PR follows.

Environment/Versions

  • @temporalio/openai-agents 1.24.0 (npm, latest) and ai-integrations main at c664a34
  • @temporalio/client, @temporalio/worker, @temporalio/workflow 1.24.0
  • @openai/agents-core 0.14.3
  • Node 24, Temporal CLI dev server

Activity

  1. added
    bug
    on Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions