You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Conditionally enable tools based on tool call history #1877
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.
@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.
fromdataclassesimportdataclassfromagentsimportAgent, function_tool, Runner,RunContextWrapper, AgentBasedefis_bar_enabled(ctx: RunContextWrapper, agent: AgentBase) ->bool:
"""Check if bar tool should be enabled based on whether foo was called."""returnctx.context.executed_tools.get("foo", False)
@function_toolasyncdeffoo(ctx: RunContextWrapper) ->str:
ctx.context.executed_tools["foo"] =Truereturn"41"@function_tool(is_enabled=is_bar_enabled)asyncdefbar() ->str:
return"64"@dataclassclassRunContext:
executed_tools: dict[str, bool]
agent=Agent[RunContext](
name="Assistant",
instructions="You are a helpful assistant. 用中文回答.",
model="gpt-4.1-mini",
tools=[foo, bar]
)
asyncdefmain():
"""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=awaitRunner.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=awaitRunner.run(agent, input=messages, context=run_context)
print(result.final_output)
if__name__=="__main__":
importasyncioasyncio.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
Activity
seratch commentedon Oct 11, 2025
Although agents as tools / handoffs of agents provide
is_enabledfunction 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:
instructions(aka system prompt) to let agents better understand your goal and requirementsThe second one may not look elegant, but it would be the most predictable design for scenarios like yours. I hope this helps!
dannyhorcajo commentedon Oct 13, 2025
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!
ihower commentedon Oct 13, 2025
@dannyhorcajo Actually, I found that regular @function_tool do support the
is_enabledparameter — 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.