Skip to content

Conditionally enable tools based on tool call history #1877

Closed

Description

@dannyhorcajo

Please read this first

  • Have you read the docs? Agents SDK docs
  • Have you searched for related issues? Others may have had similar requests

Question

I would like to dynamically enable certain tools / MCP server tools depending on whether other tools have been previously invoked. For example, query_db() should only be enabled if the agent has invoked the get_db_schema() tool previously during the conversation.

Is such behavior possible with this SDK? I have been thinking of using the run context for this, but it would get reset at each turn. I have been reading the documentation from A to Z, but I couldn't find anything of the kind.

Thank in advance!

Activity

  1. added
    question
    on Oct 9, 2025
  2. seratch commented on Oct 11, 2025

    @seratch
    Member

    Although agents as tools / handoffs of agents provide is_enabled function feature (see https://openai.github.io/openai-agents-python/tools/#conditional-tool-enabling and https://openai.github.io/openai-agents-python/handoffs/), function tools and MCP server tools do not have the same (and we don't have immediate plans to add the same to these).

    General suggestions are:

    1. Enhance your instructions (aka system prompt) to let agents better understand your goal and requirements
    2. Run a planner agent first, and then manually call required agents separately: https://github.com/openai/openai-agents-python/blob/main/examples/financial_research_agent/manager.py

    The second one may not look elegant, but it would be the most predictable design for scenarios like yours. I hope this helps!

  3. dannyhorcajo commented on Oct 13, 2025

    @dannyhorcajo
    Author

    Got it! I already had those approaches in mind, but I wanted to confirm that the feature did indeed not exist.

    Thanks for the quick answer!

  4. ihower commented on Oct 13, 2025

    @ihower
    Contributor

    @dannyhorcajo Actually, I found that regular @function_tool do support the is_enabled parameter — so it’s possible to dynamically enable or disable tools based on the RunContextWrapper data.

    Here’s a simple example: the bar tool will only be available after the foo tool has been invoked.

    from dataclasses import dataclass
    from agents import Agent, function_tool, Runner,RunContextWrapper, AgentBase
    
    def is_bar_enabled(ctx: RunContextWrapper, agent: AgentBase) -> bool:
        """Check if bar tool should be enabled based on whether foo was called."""
        return ctx.context.executed_tools.get("foo", False)
    
    @function_tool
    async def foo(ctx: RunContextWrapper) -> str:
        ctx.context.executed_tools["foo"] = True
        return "41"
    
    @function_tool(is_enabled=is_bar_enabled)
    async def bar() -> str:
        return "64"
    
    @dataclass
    class RunContext:
        executed_tools: dict[str, bool]
    
    agent = Agent[RunContext](
        name="Assistant",
        instructions="You are a helpful assistant. 用中文回答.",
        model="gpt-4.1-mini",
        tools=[foo, bar]
    )
    
    async def main():
        """Main function demonstrating conditional tool availability."""
        run_context = RunContext(executed_tools={})
    
        print("-------------------------------- Round 1 --------------------------------")
    
        messages = [{"role": "user", "content": "Call the foo tool first. After getting the result, you can call the bar tool, and finally tell me the result."}]
    
        result = await Runner.run(agent, input=messages, context=run_context)
    
        print(result.final_output)
        
        print("-------------------------------- Round 2 --------------------------------")
        
        messages = result.to_input_list()
    
        messages.append({"role": "user", "content": "Call the bar tool again and tell me the result"})
        result = await Runner.run(agent, input=messages, context=run_context)
        
        print(result.final_output)
    
    if __name__ == "__main__":
        import asyncio
        asyncio.run(main())
    
    # Result:
    # -------------------------------- Round 1 --------------------------------
    # 1st API call: Calls foo tool. Available tools: [foo]
    # 2nd API call: Calls bar tool. Available tools: [foo, bar]
    # 3rd API call: Returns the final text answer
    
    # -------------------------------- Round 2 --------------------------------
    # 1st API call: Calls bar tool. Available tools: [foo, bar]
    # 2nd API call: Returns the final text answer
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

    feature:corefeature:mcpquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions