@@ -480,6 +480,9 @@ pub(crate) struct Session {
480480 next_internal_sub_id : AtomicU64 ,
481481}
482482
483+ const SEARCH_TOOL_DEVELOPER_INSTRUCTIONS : & str =
484+ include_str ! ( "../templates/search_tool/developer_instructions.md" ) ;
485+
483486/// The context needed for a single turn of the thread.
484487#[ derive( Debug ) ]
485488pub ( crate ) struct TurnContext {
@@ -1104,6 +1107,16 @@ impl Session {
11041107 }
11051108 }
11061109
1110+ pub ( crate ) async fn set_next_mcp_tool_selection ( & self , tool_names : Vec < String > ) {
1111+ let mut state = self . state . lock ( ) . await ;
1112+ state. set_next_mcp_tool_selection ( tool_names) ;
1113+ }
1114+
1115+ pub ( crate ) async fn take_next_mcp_tool_selection ( & self ) -> Option < Vec < String > > {
1116+ let mut state = self . state . lock ( ) . await ;
1117+ state. take_next_mcp_tool_selection ( )
1118+ }
1119+
11071120 async fn record_initial_history ( & self , conversation_history : InitialHistory ) {
11081121 let turn_context = self . new_default_turn ( ) . await ;
11091122 match conversation_history {
@@ -1945,7 +1958,7 @@ impl Session {
19451958 & self ,
19461959 turn_context : & TurnContext ,
19471960 ) -> Vec < ResponseItem > {
1948- let mut items = Vec :: < ResponseItem > :: with_capacity ( 4 ) ;
1961+ let mut items = Vec :: < ResponseItem > :: with_capacity ( 6 ) ;
19491962 let shell = self . user_shell ( ) ;
19501963 items. push (
19511964 DeveloperInstructions :: from_policy (
@@ -1960,6 +1973,11 @@ impl Session {
19601973 if let Some ( developer_instructions) = turn_context. developer_instructions . as_deref ( ) {
19611974 items. push ( DeveloperInstructions :: new ( developer_instructions. to_string ( ) ) . into ( ) ) ;
19621975 }
1976+ if turn_context. tools_config . search_tool {
1977+ items. push (
1978+ DeveloperInstructions :: new ( SEARCH_TOOL_DEVELOPER_INSTRUCTIONS . to_string ( ) ) . into ( ) ,
1979+ ) ;
1980+ }
19631981 // Add developer instructions from collaboration_mode if they exist and are non-empty
19641982 let ( collaboration_mode, base_instructions) = {
19651983 let state = self . state . lock ( ) . await ;
@@ -3779,6 +3797,15 @@ fn filter_codex_apps_mcp_tools(
37793797 mcp_tools
37803798}
37813799
3800+ fn filter_mcp_tools_by_name (
3801+ mut mcp_tools : HashMap < String , crate :: mcp_connection_manager:: ToolInfo > ,
3802+ selected_tools : & [ String ] ,
3803+ ) -> HashMap < String , crate :: mcp_connection_manager:: ToolInfo > {
3804+ let allowed: HashSet < & str > = selected_tools. iter ( ) . map ( String :: as_str) . collect ( ) ;
3805+ mcp_tools. retain ( |name, _| allowed. contains ( name. as_str ( ) ) ) ;
3806+ mcp_tools
3807+ }
3808+
37823809fn codex_apps_connector_id ( tool : & crate :: mcp_connection_manager:: ToolInfo ) -> Option < & str > {
37833810 tool. connector_id . as_deref ( )
37843811}
@@ -3813,19 +3840,31 @@ async fn run_sampling_request(
38133840 . list_all_tools ( )
38143841 . or_cancel ( & cancellation_token)
38153842 . await ?;
3816- let connectors_for_tools = if turn_context. config . features . enabled ( Feature :: Apps ) {
3817- let connectors = connectors:: accessible_connectors_from_mcp_tools ( & mcp_tools) ;
3818- Some ( filter_connectors_for_input (
3819- connectors,
3820- & input,
3821- tool_selection. explicit_app_paths ,
3822- tool_selection. skill_name_counts_lower ,
3823- ) )
3843+ let search_tool_enabled = turn_context
3844+ . config
3845+ . features
3846+ . enabled ( Feature :: SearchTool ) ;
3847+ if search_tool_enabled {
3848+ if let Some ( selected_tools) = sess. take_next_mcp_tool_selection ( ) . await {
3849+ mcp_tools = filter_mcp_tools_by_name ( mcp_tools, & selected_tools) ;
3850+ } else {
3851+ mcp_tools. clear ( ) ;
3852+ }
38243853 } else {
3825- None
3826- } ;
3827- if let Some ( connectors) = connectors_for_tools. as_ref ( ) {
3828- mcp_tools = filter_codex_apps_mcp_tools ( mcp_tools, connectors) ;
3854+ let connectors_for_tools = if turn_context. config . features . enabled ( Feature :: Apps ) {
3855+ let connectors = connectors:: accessible_connectors_from_mcp_tools ( & mcp_tools) ;
3856+ Some ( filter_connectors_for_input (
3857+ connectors,
3858+ & input,
3859+ tool_selection. explicit_app_paths ,
3860+ tool_selection. skill_name_counts_lower ,
3861+ ) )
3862+ } else {
3863+ None
3864+ } ;
3865+ if let Some ( connectors) = connectors_for_tools. as_ref ( ) {
3866+ mcp_tools = filter_codex_apps_mcp_tools ( mcp_tools, connectors) ;
3867+ }
38293868 }
38303869 let router = Arc :: new ( ToolRouter :: from_config (
38313870 & turn_context. tools_config ,
0 commit comments