4242 config = AgenticACPConfig (type = "base" ),
4343)
4444
45+
4546class StateModel (BaseModel ):
4647 input_list : List [dict ]
4748 turn_number : int
@@ -53,11 +54,7 @@ class StateModel(BaseModel):
5354 args = ["-y" , "@modelcontextprotocol/server-sequential-thinking" ],
5455 ),
5556 StdioServerParameters (
56- command = "uvx" ,
57- args = ["openai-websearch-mcp" ],
58- env = {
59- "OPENAI_API_KEY" : os .environ .get ("OPENAI_API_KEY" , "" )
60- }
57+ command = "uvx" , args = ["openai-websearch-mcp" ], env = {"OPENAI_API_KEY" : os .environ .get ("OPENAI_API_KEY" , "" )}
6158 ),
6259]
6360
@@ -72,6 +69,7 @@ async def handle_task_create(params: CreateTaskParams):
7269 )
7370 await adk .state .create (task_id = params .task .id , agent_id = params .agent .id , state = state )
7471
72+
7573@acp .on_task_event_send
7674async def handle_event_send (params : SendEventParams ):
7775 # !!! Warning: Because "Agentic" ACPs are designed to be fully asynchronous, race conditions can occur if parallel events are sent. It is highly recommended to use the "temporal" type in the AgenticACPConfig instead to handle complex use cases. The "base" ACP is only designed to be used for simple use cases and for learning purposes.
@@ -85,7 +83,6 @@ async def handle_event_send(params: SendEventParams):
8583 if params .event .content .author != "user" :
8684 raise ValueError (f"Expected user message, got { params .event .content .author } " )
8785
88-
8986 # Retrieve the task state. Each event is handled as a new turn, so we need to get the state for the current turn.
9087 task_state = await adk .state .get_by_task_and_agent (task_id = params .task .id , agent_id = params .agent .id )
9188 if not task_state :
@@ -94,12 +91,8 @@ async def handle_event_send(params: SendEventParams):
9491 state .turn_number += 1
9592 # Add the new user message to the message history
9693 state .input_list .append ({"role" : "user" , "content" : params .event .content .content })
97-
98- async with adk .tracing .span (
99- trace_id = params .task .id ,
100- name = f"Turn { state .turn_number } " ,
101- input = state
102- ) as span :
94+
95+ async with adk .tracing .span (trace_id = params .task .id , name = f"Turn { state .turn_number } " , input = state ) as span :
10396 # Echo back the user's message so it shows up in the UI. This is not done by default so the agent developer has full control over what is shown to the user.
10497 await adk .messages .create (
10598 task_id = params .task .id ,
@@ -156,6 +149,7 @@ async def handle_event_send(params: SendEventParams):
156149 if span :
157150 span .output = state
158151
152+
159153@acp .on_task_cancel
160154async def handle_task_cancel (params : CancelTaskParams ):
161155 """Default task cancel handler"""
@@ -173,8 +167,8 @@ async def mcp_server_context(mcp_server_params: list[StdioServerParameters]):
173167 servers = []
174168 for params in mcp_server_params :
175169 server = MCPServerStdio (
176- name = f"Server: { params .command } " ,
177- params = params .model_dump (),
170+ name = f"Server: { params .command } " ,
171+ params = params .model_dump (),
178172 cache_tools_list = True ,
179173 client_session_timeout_seconds = 60 ,
180174 )
@@ -253,7 +247,6 @@ async def run_openai_agent_with_custom_streaming(
253247 try :
254248 # Process streaming events with TaskMessage creation
255249 async for event in result .stream_events ():
256-
257250 if event .type == "run_item_stream_event" :
258251 if event .item .type == "tool_call_item" :
259252 tool_call_item = event .item .raw_item
@@ -374,9 +367,7 @@ async def run_openai_agent_with_custom_streaming(
374367 if span :
375368 span .output = {
376369 "new_items" : [
377- item .raw_item .model_dump ()
378- if isinstance (item .raw_item , BaseModel )
379- else item .raw_item
370+ item .raw_item .model_dump () if isinstance (item .raw_item , BaseModel ) else item .raw_item
380371 for item in result .new_items
381372 ],
382373 "final_output" : result .final_output ,
0 commit comments