Skip to content

Python: Fix to include MCP-related events in streaming chunks of OpenAI Responses Agent#13112

Closed
ymuichiro wants to merge 5 commits into
microsoft:mainfrom
ymuichiro:feat/add-mcp-specific-streaming-support-for-argument-events
Closed

Python: Fix to include MCP-related events in streaming chunks of OpenAI Responses Agent#13112
ymuichiro wants to merge 5 commits into
microsoft:mainfrom
ymuichiro:feat/add-mcp-specific-streaming-support-for-argument-events

Conversation

@ymuichiro

@ymuichiro ymuichiro commented Sep 12, 2025

Copy link
Copy Markdown
Contributor

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

Copilot AI review requested due to automatic review settings September 12, 2025 10:11
@ymuichiro ymuichiro requested a review from a team as a code owner September 12, 2025 10:11
@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label Sep 12, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread python/semantic_kernel/agents/open_ai/responses_agent_thread_actions.py Outdated
@yuichiromukaiyama

yuichiromukaiyama commented Sep 12, 2025

Copy link
Copy Markdown
Contributor

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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd need to add similar handling for the non-streaming case, correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moonbox3 I tried making changes.
5c683c5

@moonbox3

Copy link
Copy Markdown
Collaborator

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
agents/open_ai
   responses_agent_thread_actions.py46914768%179, 196, 209, 220–221, 229, 236, 249, 411–412, 414, 418, 424, 446, 457, 464, 466, 468–470, 472, 486, 493, 514–515, 527, 534, 536–537, 548, 555, 557–558, 571, 578, 580–581, 593, 600, 610–612, 615–620, 623, 638, 645, 650–651, 655, 667, 701–703, 706, 724–725, 736–738, 742, 746–747, 751–753, 764–766, 788, 790–791, 798–799, 801–802, 804, 806–807, 809–810, 922, 927, 935–936, 938, 944, 949, 954–956, 962–964, 969–970, 972–977, 979, 983–984, 986, 990, 994, 999–1000, 1005, 1008–1009, 1069–1072, 1095, 1141–1144, 1185–1186, 1190–1192, 1194, 1219–1220, 1222–1226, 1228, 1238–1239, 1247, 1251, 1258, 1264, 1340
TOTAL27152474882% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3720 22 💤 0 ❌ 0 🔥 1m 39s ⏱️

@ymuichiro

Copy link
Copy Markdown
Contributor Author

I made a fix!

@moonbox3

moonbox3 commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

Sorry for the super delay on this, @ymuichiro. Is this still needed?

@ymuichiro

Copy link
Copy Markdown
Contributor Author

@moonbox3

Thank you for checking!

At this point, I’ve already started migrating to a different SDK,
so this fix is no longer needed. At the time,

I addressed the issue by applying a local patch on my side.

Feel free to close this without merging. I appreciate your support!

@moonbox3

Copy link
Copy Markdown
Collaborator

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!)

@moonbox3 moonbox3 closed this Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants