Python: Fix to include MCP-related events in streaming chunks of OpenAI Responses Agent#13112
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes MCP-related events handling in the OpenAI Responses Agent streaming functionality to ensure they are properly detected and processed by on_intermediate_message.
- Adds support for tracking and processing MCP tool calls during streaming
- Implements consistent metadata handling across all streaming events
- Creates dedicated handlers for MCP call arguments and completion events
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
output example import asyncio
import os
from dotenv import load_dotenv
from openai import AsyncAzureOpenAI
from semantic_kernel.agents import AzureResponsesAgent
from semantic_kernel.contents import ChatMessageContent
from semantic_kernel.core_plugins import MathPlugin
load_dotenv()
AZURE_OPENAI_RESPONSES_MODEL_ID: str = os.getenv("AZURE_OPENAI_RESPONSES_MODEL_ID", "")
client: AsyncAzureOpenAI = AzureResponsesAgent.create_client(deployment_name=AZURE_OPENAI_RESPONSES_MODEL_ID)
async def handle_intermediate_message(message: ChatMessageContent):
# Accepts streaming or regular chat message content; be defensive about shape
for item in message.items:
print(item.model_dump_json(exclude_none=True))
async def main():
agent = AzureResponsesAgent(
ai_model_id=AZURE_OPENAI_RESPONSES_MODEL_ID,
client=client,
name="Agent",
plugins=[MathPlugin()],
tools=[
{
"type": "mcp",
"server_label": "microsoft_document_label",
"server_url": "https://learn.microsoft.com/api/mcp",
"require_approval": "never",
}
],
)
thread = None
user_prompt = "xxxxxxxxx"
print("reasoning start: ", user_prompt)
async for r in agent.invoke_stream(
messages=user_prompt,
thread=thread,
on_intermediate_message=handle_intermediate_message,
):
print(r, end="", flush=True)
thread = r.thread
if __name__ == "__main__":
asyncio.run(main()){"metadata":{"event_type":"response.mcp_call_arguments.done","sequence_number":5,"output_index":0},"content_type":"function_call","id":"mcp_68c3e04c2e7c8195965bed013351174808202fcc162ea04d","index":0,"name":"microsoft_docs_search","function_name":"microsoft_docs_search","plugin_name":"microsoft_document_label","arguments":"{\"query\":\"azure ai agent\",\"question\":\"azure ai agent\"}"}
{"metadata":{"used_arguments":{"query":"azure ai agent","question":"azure ai agent"},"arguments":{"query":"azure ai agent","question":"azure ai agent"},"event_type":"response.output_item.done"},"content_type":"function_result","id":"mcp_68c3e04c2e7c8195965bed013351174808202fcc162ea04d","result":"[{\"title\": \"Trans ... ocess\"}]","name":"microsoft_docs_search","function_name":"microsoft_docs_search","plugin_name":"microsoft_document_label"}
{"metadata":{"event_type":"response.function_call_arguments.delta","sequence_number":15,"output_index":1},"content_type":"function_call","id":"fc_68c3e04b00988195a3293b76cdcdfda608202fcc162ea04d","call_id":"call_MVxi33ITMjP7RV9JVwNpybFV","name":"MathPlugin-Add","function_name":"Add","plugin_name":"MathPlugin","arguments":"{\"input\":2,\"amount\":2}"}
{"metadata":{"event_type":"response.function_call_arguments.delta","sequence_number":15,"output_index":1,"arguments":{"input":2,"amount":2},"used_arguments":{"input":2,"amount":2}},"content_type":"function_result","id":"fc_68c3e04b00988195a3293b76cdcdfda608202fcc162ea04d","call_id":"call_MVxi33ITMjP7RV9JVwNpybFV","result":"4.0","name":"MathPlugin-Add","function_name":"Add","plugin_name":"MathPlugin"} |
| tool_name: str = tool_info.get("name", "") | ||
| server_label: str = tool_info.get("server_label", "") | ||
|
|
||
| mcp_args_done = FunctionCallContent( |
There was a problem hiding this comment.
We'd need to add similar handling for the non-streaming case, correct?
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
I made a fix! |
|
Sorry for the super delay on this, @ymuichiro. Is this still needed? |
|
Thank you for checking! At this point, I’ve already started migrating to a different SDK, I addressed the issue by applying a local patch on my side. Feel free to close this without merging. I appreciate your support! |
|
Sounds good, @ymuichiro. Glad you found a path forward. Closing it out now. If you're open to it, I'd love to hear what led you to migrate and where the SDK fell short. Feedback like that is some of the most useful signal we get. Feel free to reach out on LinkedIn (linked from my GitHub profile) if you'd rather chat there. No pressure either way. (And if the migration was to our Microsoft Agent Framework, welcome!) |
Motivation and Context
When using the OpenAI Responses API, MCP-related events were not being detected by
on_intermediate_message. This has been fixed so that the relevant events are now included.Additionally, the usage of metadata for each event was inconsistent, and some information such as event type was missing, making it difficult for the caller to determine which event each chunk belonged to.
This fix was made based on the following issue.
#13085
Contribution Checklist