openai.responses_tool()

Connect Console to an application-owned OpenAI Responses tool loop.

Usage

Source

openai.responses_tool(console: MCPConsole) -> ResponsesTool
openai.responses_tool(console: AsyncMCPConsole) -> AsyncResponsesTool

Pass tool.definition to responses.create(tools=...). When the model returns a send function call, tool(call) executes it and returns the function_call_output item for the next API request. Text and images are preserved. The application runs this loop and routes any other tools.

Create the adapter after connecting Console, and keep that connection open for the entire loop. With AsyncMCPConsole, the factory returns an AsyncResponsesTool; await both the model requests and tool(call).

Parameters

console: MCPConsole | AsyncMCPConsole
A connected MCPConsole or AsyncMCPConsole.

Returns

ResponsesTool | AsyncResponsesTool
A ResponsesTool or AsyncResponsesTool matching the client.

Examples

Install with pip install 'mcp-console[openai]'. Set OPENAI_API_KEY and set OPENAI_MODEL to your model ID. This example registers only Console, feeds each tool result back into the response chain, and prints the final answer:

import os
import mcp_console
from mcp_console import MCPConsole
from openai import OpenAI

with OpenAI() as client, MCPConsole() as console:
    tool = mcp_console.openai.responses_tool(console)
    response = client.responses.create(
        model=os.environ["OPENAI_MODEL"],
        input="Use Python in Console to calculate the mean and sample "
        "standard deviation of [12, 15, 18, 20, 25].",
        tools=[tool.definition],
        parallel_tool_calls=False,
    )
    while calls := [
        item for item in response.output if item.type == "function_call"
    ]:
        response = client.responses.create(
            model=os.environ["OPENAI_MODEL"],
            previous_response_id=response.id,
            input=[tool(call) for call in calls],
            tools=[tool.definition],
            parallel_tool_calls=False,
        )
    print(response.output_text)

previous_response_id keeps the conversation, including earlier tool calls. parallel_tool_calls=False requests sequential calls to the persistent Console. Keep passing the tool definition on each request.