From a923175cdde93ec27fcedd59696264b3dbdc6e06 Mon Sep 17 00:00:00 2001 From: dpiet-oai <326640584+dpiet-oai@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:44:11 +0000 Subject: [PATCH] feat(api): add Live MCP server configuration Castiron-Internal-PR: https://github.com/openai/openai-python-internal/pull/411 Castiron-Source-SHA: 0faad906564546b2abb7c7834421469f526fb2c1 Castiron-Public-Base-SHA: f668458323fb48228e3adef37232550de2606ea7 --- .castiron.stats.yml | 12 +- api_reference/openapi.transformed.yml | 115 ++++++++++++++++++ scripts/castiron/generated-files.json | 8 +- .../types/live/responses_delegation_config.py | 90 +++++++++++++- .../live/responses_delegation_config_param.py | 90 +++++++++++++- .../responses_delegation_update_config.py | 90 +++++++++++++- ...esponses_delegation_update_config_param.py | 90 +++++++++++++- tests/lib/live/test_mcp_transport.py | 104 ++++++++++++++++ 8 files changed, 585 insertions(+), 14 deletions(-) create mode 100644 tests/lib/live/test_mcp_transport.py diff --git a/.castiron.stats.yml b/.castiron.stats.yml index 041e6d9adf..245201d8ed 100644 --- a/.castiron.stats.yml +++ b/.castiron.stats.yml @@ -1,7 +1,7 @@ schema_version: 1 -generation_id: 58c21476-12d6-468a-8b78-cd527cb0c36b -openapi_spec_hash: f9e75496fa8fa86dd76892908bee2d8a -openapi_transformed_spec_hash: 93d40780edbc80f337b088768581497e -config_hash: dc8ed14ee6b29b10b22e1135ee79d527 -codegen_sha: 69e9b09ef11d246e1cebce2f444dcf13150d79d3 -codegen_hash: 1105715de1093af4ab7c8bba5baae70aab0b256d81acea47913f360e2b5d97b3 +generation_id: e0db1179-fe12-4bb2-bd87-8631bb67d669 +openapi_spec_hash: c09a996bda1329932cb3cdbc62ac2bc4 +openapi_transformed_spec_hash: 877730e5c258cc3abc2cc03f2b864cbe +config_hash: 75d0b851f817c2f6384c562a88468286 +codegen_sha: 4f825036970380029f250cf929e9cc688cf3f0ad +codegen_hash: 397c8644ff0580d3d2b9b974979b1679fa1fa52b0063bd2dfe2c2fe6254a88a7 diff --git a/api_reference/openapi.transformed.yml b/api_reference/openapi.transformed.yml index 60ad717da8..cce877f050 100644 --- a/api_reference/openapi.transformed.yml +++ b/api_reference/openapi.transformed.yml @@ -80002,8 +80002,83 @@ components: type: string enum: - mcp + server_label: + description: A label used to identify the MCP server. + type: string + maxLength: 128 + pattern: ^[A-Za-z][A-Za-z0-9_-]*$ + server_url: + anyOf: + - description: The URL of the MCP server to connect to. + type: string + format: uri + - type: 'null' + server_description: + anyOf: + - description: An optional human-readable description of the MCP server. + type: string + - type: 'null' + authorization: + description: This value is omitted from returned Live session data. + writeOnly: true + anyOf: + - description: The authorization token to use when connecting to the MCP server or connector. + type: string + - type: 'null' + headers: + description: This value is omitted from returned Live session data. + writeOnly: true + anyOf: + - description: Optional HTTP headers to send when connecting to the MCP server. + type: object + additionalProperties: + type: string + - type: 'null' + allowed_tools: + anyOf: + - description: A list of MCP tool names that are allowed for this server. + anyOf: + - type: array + items: + type: string + - $ref: '#/components/schemas/LiveMCPToolFilterParam' + - type: 'null' + require_approval: + anyOf: + - description: Controls which MCP tools require explicit user approval before execution. + anyOf: + - $ref: '#/components/schemas/LiveMCPRequireApprovalFilterParam' + - description: Default approval behavior for MCP tool calls. + $ref: '#/components/schemas/LiveMCPRequireApprovalApiEnum' + - type: 'null' + connector_id: + anyOf: + - description: The ID of a built-in MCP connector. This field is deprecated for models released after September 1, 2026. Use `server_url` to connect to a remote MCP server, or `tunnel_id` to connect to a local MCP server. + deprecated: true + type: string + - type: 'null' + tunnel_id: + anyOf: + - description: The Secure MCP Tunnel ID to use instead of a direct server URL. + type: string + pattern: ^tunnel_[a-z0-9]{32}$ + - type: 'null' + allowed_callers: + anyOf: + - description: The tool invocation context(s). + type: array + items: + $ref: '#/components/schemas/LiveCallableToolAllowedCaller' + minItems: 1 + - type: 'null' + defer_loading: + anyOf: + - description: Whether this MCP tool should be deferred and discovered via tool search. + type: boolean + - type: 'null' required: - type + x-stainless-go-constant-constructor: true LiveCustomToolInputParam: type: object properties: @@ -113361,6 +113436,46 @@ components: - consumed additionalProperties: false description: Session-wide spending limit and best-effort consumption, including unlimited sessions. + LiveMCPToolFilterParam: + type: object + properties: + tool_names: + anyOf: + - description: A list of tool names to match against. + type: array + items: + type: string + - type: 'null' + read_only: + anyOf: + - description: Whether to allow only read-only tools for matching entries. + type: boolean + - type: 'null' + required: [] + LiveMCPRequireApprovalFilterParam: + type: object + properties: + always: + anyOf: + - description: A filter that forces approval for matching tools. + $ref: '#/components/schemas/LiveMCPToolFilterParam' + - type: 'null' + never: + anyOf: + - description: A filter that skips approval for matching tools. + $ref: '#/components/schemas/LiveMCPToolFilterParam' + - type: 'null' + required: [] + LiveMCPRequireApprovalApiEnum: + type: string + enum: + - always + - never + LiveCallableToolAllowedCaller: + type: string + enum: + - direct + - programmatic DecisionPromptCacheBreakpoint: type: object properties: diff --git a/scripts/castiron/generated-files.json b/scripts/castiron/generated-files.json index 0e0976fe9b..7665223870 100644 --- a/scripts/castiron/generated-files.json +++ b/scripts/castiron/generated-files.json @@ -5051,19 +5051,19 @@ }, "src/openai/types/live/responses_delegation_config.py": { "mode": "100644", - "sha256": "9acf81a3c59c90ea84fa44ae0623bda33c7c8b55b814713ec45242c5027a7c0f" + "sha256": "cecbecc89143eb9484f1d550cd16602270e4488c88bc114b22555ada9147e899" }, "src/openai/types/live/responses_delegation_config_param.py": { "mode": "100644", - "sha256": "033eb7569fa9975bd2adb584f645581067b068f8ff70bf93f5848278c3b02d75" + "sha256": "ae629693e0c0208368edd560257892b330d84f2df35738c582675ba8c5e0a573" }, "src/openai/types/live/responses_delegation_update_config.py": { "mode": "100644", - "sha256": "c0e958d70b97fe6fe67c5ae0c8bfb8b89d0a56cfccb36d0e1573faafa9334112" + "sha256": "3f5bd2717ab74e33c2d31f4925a8241643b0bf3b2d8019823238089d02ee579d" }, "src/openai/types/live/responses_delegation_update_config_param.py": { "mode": "100644", - "sha256": "5a13bebcc51dff57350011a07333209ce359c9e47e7f219f8c8d0ec6ecf1dfbc" + "sha256": "6b07ccfbbaa66b0f5641e12e511130abd162bdbb7a3ee6e5c6c6613c06b54bde" }, "src/openai/types/live/server_event.py": { "mode": "100644", diff --git a/src/openai/types/live/responses_delegation_config.py b/src/openai/types/live/responses_delegation_config.py index dafa065af5..3083826e1b 100644 --- a/src/openai/types/live/responses_delegation_config.py +++ b/src/openai/types/live/responses_delegation_config.py @@ -1,6 +1,6 @@ # File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details. -from typing import List, Union, Optional +from typing import Dict, List, Union, Optional from typing_extensions import Literal, Annotated, TypeAlias from ..._utils import PropertyInfo @@ -57,6 +57,12 @@ "ToolShellEnvironmentLocalSkill", "ToolImageGeneration", "ToolMcp", + "ToolMcpAllowedTools", + "ToolMcpAllowedToolsLiveMCPToolFilterParam", + "ToolMcpRequireApproval", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever", "ToolCustom", "ToolNamespace", "ToolSearch", @@ -430,9 +436,91 @@ class ToolImageGeneration(BaseModel): type: Literal["image_generation"] +class ToolMcpAllowedToolsLiveMCPToolFilterParam(BaseModel): + read_only: Optional[bool] = None + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[List[str]] = None + """A list of tool names to match against.""" + + +ToolMcpAllowedTools: TypeAlias = Union[List[str], ToolMcpAllowedToolsLiveMCPToolFilterParam, None] + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways(BaseModel): + """A filter that forces approval for matching tools.""" + + read_only: Optional[bool] = None + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[List[str]] = None + """A list of tool names to match against.""" + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever(BaseModel): + """A filter that skips approval for matching tools.""" + + read_only: Optional[bool] = None + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[List[str]] = None + """A list of tool names to match against.""" + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam(BaseModel): + always: Optional[ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways] = None + """A filter that forces approval for matching tools.""" + + never: Optional[ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever] = None + """A filter that skips approval for matching tools.""" + + +ToolMcpRequireApproval: TypeAlias = Union[ + ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam, Literal["always", "never"], None +] + + class ToolMcp(BaseModel): type: Literal["mcp"] + allowed_callers: Optional[List[Literal["direct", "programmatic"]]] = None + """The tool invocation context(s).""" + + allowed_tools: Optional[ToolMcpAllowedTools] = None + """A list of MCP tool names that are allowed for this server.""" + + authorization: Optional[str] = None + """This value is omitted from returned Live session data.""" + + connector_id: Optional[str] = None + """The ID of a built-in MCP connector. + + This field is deprecated for models released after September 1, 2026. Use + `server_url` to connect to a remote MCP server, or `tunnel_id` to connect to a + local MCP server. + """ + + defer_loading: Optional[bool] = None + """Whether this MCP tool should be deferred and discovered via tool search.""" + + headers: Optional[Dict[str, str]] = None + """Optional HTTP headers to send when connecting to the MCP server.""" + + require_approval: Optional[ToolMcpRequireApproval] = None + """Controls which MCP tools require explicit user approval before execution.""" + + server_description: Optional[str] = None + """An optional human-readable description of the MCP server.""" + + server_label: Optional[str] = None + """A label used to identify the MCP server.""" + + server_url: Optional[str] = None + """The URL of the MCP server to connect to.""" + + tunnel_id: Optional[str] = None + """The Secure MCP Tunnel ID to use instead of a direct server URL.""" + class ToolCustom(BaseModel): type: Literal["custom"] diff --git a/src/openai/types/live/responses_delegation_config_param.py b/src/openai/types/live/responses_delegation_config_param.py index 40a37f51bc..f7aae559b9 100644 --- a/src/openai/types/live/responses_delegation_config_param.py +++ b/src/openai/types/live/responses_delegation_config_param.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Union, Iterable, Optional +from typing import Dict, List, Union, Iterable, Optional from typing_extensions import Literal, Required, TypeAlias, TypedDict from ..._types import SequenceNotStr @@ -58,6 +58,12 @@ "ToolShellEnvironmentLocalSkill", "ToolImageGeneration", "ToolMcp", + "ToolMcpAllowedTools", + "ToolMcpAllowedToolsLiveMCPToolFilterParam", + "ToolMcpRequireApproval", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever", "ToolCustom", "ToolNamespace", "ToolSearch", @@ -421,9 +427,91 @@ class ToolImageGeneration(TypedDict, total=False): type: Required[Literal["image_generation"]] +class ToolMcpAllowedToolsLiveMCPToolFilterParam(TypedDict, total=False): + read_only: Optional[bool] + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[SequenceNotStr[str]] + """A list of tool names to match against.""" + + +ToolMcpAllowedTools: TypeAlias = Union[SequenceNotStr[str], ToolMcpAllowedToolsLiveMCPToolFilterParam] + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways(TypedDict, total=False): + """A filter that forces approval for matching tools.""" + + read_only: Optional[bool] + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[SequenceNotStr[str]] + """A list of tool names to match against.""" + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever(TypedDict, total=False): + """A filter that skips approval for matching tools.""" + + read_only: Optional[bool] + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[SequenceNotStr[str]] + """A list of tool names to match against.""" + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam(TypedDict, total=False): + always: Optional[ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways] + """A filter that forces approval for matching tools.""" + + never: Optional[ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever] + """A filter that skips approval for matching tools.""" + + +ToolMcpRequireApproval: TypeAlias = Union[ + ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam, Literal["always", "never"] +] + + class ToolMcp(TypedDict, total=False): type: Required[Literal["mcp"]] + allowed_callers: Optional[List[Literal["direct", "programmatic"]]] + """The tool invocation context(s).""" + + allowed_tools: Optional[ToolMcpAllowedTools] + """A list of MCP tool names that are allowed for this server.""" + + authorization: Optional[str] + """This value is omitted from returned Live session data.""" + + connector_id: Optional[str] + """The ID of a built-in MCP connector. + + This field is deprecated for models released after September 1, 2026. Use + `server_url` to connect to a remote MCP server, or `tunnel_id` to connect to a + local MCP server. + """ + + defer_loading: Optional[bool] + """Whether this MCP tool should be deferred and discovered via tool search.""" + + headers: Optional[Dict[str, str]] + """Optional HTTP headers to send when connecting to the MCP server.""" + + require_approval: Optional[ToolMcpRequireApproval] + """Controls which MCP tools require explicit user approval before execution.""" + + server_description: Optional[str] + """An optional human-readable description of the MCP server.""" + + server_label: str + """A label used to identify the MCP server.""" + + server_url: Optional[str] + """The URL of the MCP server to connect to.""" + + tunnel_id: Optional[str] + """The Secure MCP Tunnel ID to use instead of a direct server URL.""" + class ToolCustom(TypedDict, total=False): type: Required[Literal["custom"]] diff --git a/src/openai/types/live/responses_delegation_update_config.py b/src/openai/types/live/responses_delegation_update_config.py index 89313284d2..8bd58cd5c9 100644 --- a/src/openai/types/live/responses_delegation_update_config.py +++ b/src/openai/types/live/responses_delegation_update_config.py @@ -1,6 +1,6 @@ # File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details. -from typing import List, Union, Optional +from typing import Dict, List, Union, Optional from typing_extensions import Literal, Annotated, TypeAlias from ..._utils import PropertyInfo @@ -57,6 +57,12 @@ "ToolShellEnvironmentLocalSkill", "ToolImageGeneration", "ToolMcp", + "ToolMcpAllowedTools", + "ToolMcpAllowedToolsLiveMCPToolFilterParam", + "ToolMcpRequireApproval", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever", "ToolCustom", "ToolNamespace", "ToolSearch", @@ -430,9 +436,91 @@ class ToolImageGeneration(BaseModel): type: Literal["image_generation"] +class ToolMcpAllowedToolsLiveMCPToolFilterParam(BaseModel): + read_only: Optional[bool] = None + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[List[str]] = None + """A list of tool names to match against.""" + + +ToolMcpAllowedTools: TypeAlias = Union[List[str], ToolMcpAllowedToolsLiveMCPToolFilterParam, None] + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways(BaseModel): + """A filter that forces approval for matching tools.""" + + read_only: Optional[bool] = None + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[List[str]] = None + """A list of tool names to match against.""" + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever(BaseModel): + """A filter that skips approval for matching tools.""" + + read_only: Optional[bool] = None + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[List[str]] = None + """A list of tool names to match against.""" + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam(BaseModel): + always: Optional[ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways] = None + """A filter that forces approval for matching tools.""" + + never: Optional[ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever] = None + """A filter that skips approval for matching tools.""" + + +ToolMcpRequireApproval: TypeAlias = Union[ + ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam, Literal["always", "never"], None +] + + class ToolMcp(BaseModel): type: Literal["mcp"] + allowed_callers: Optional[List[Literal["direct", "programmatic"]]] = None + """The tool invocation context(s).""" + + allowed_tools: Optional[ToolMcpAllowedTools] = None + """A list of MCP tool names that are allowed for this server.""" + + authorization: Optional[str] = None + """This value is omitted from returned Live session data.""" + + connector_id: Optional[str] = None + """The ID of a built-in MCP connector. + + This field is deprecated for models released after September 1, 2026. Use + `server_url` to connect to a remote MCP server, or `tunnel_id` to connect to a + local MCP server. + """ + + defer_loading: Optional[bool] = None + """Whether this MCP tool should be deferred and discovered via tool search.""" + + headers: Optional[Dict[str, str]] = None + """Optional HTTP headers to send when connecting to the MCP server.""" + + require_approval: Optional[ToolMcpRequireApproval] = None + """Controls which MCP tools require explicit user approval before execution.""" + + server_description: Optional[str] = None + """An optional human-readable description of the MCP server.""" + + server_label: Optional[str] = None + """A label used to identify the MCP server.""" + + server_url: Optional[str] = None + """The URL of the MCP server to connect to.""" + + tunnel_id: Optional[str] = None + """The Secure MCP Tunnel ID to use instead of a direct server URL.""" + class ToolCustom(BaseModel): type: Literal["custom"] diff --git a/src/openai/types/live/responses_delegation_update_config_param.py b/src/openai/types/live/responses_delegation_update_config_param.py index 1143e62499..ac019110f4 100644 --- a/src/openai/types/live/responses_delegation_update_config_param.py +++ b/src/openai/types/live/responses_delegation_update_config_param.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Union, Iterable, Optional +from typing import Dict, List, Union, Iterable, Optional from typing_extensions import Literal, Required, TypeAlias, TypedDict from ..._types import SequenceNotStr @@ -58,6 +58,12 @@ "ToolShellEnvironmentLocalSkill", "ToolImageGeneration", "ToolMcp", + "ToolMcpAllowedTools", + "ToolMcpAllowedToolsLiveMCPToolFilterParam", + "ToolMcpRequireApproval", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways", + "ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever", "ToolCustom", "ToolNamespace", "ToolSearch", @@ -421,9 +427,91 @@ class ToolImageGeneration(TypedDict, total=False): type: Required[Literal["image_generation"]] +class ToolMcpAllowedToolsLiveMCPToolFilterParam(TypedDict, total=False): + read_only: Optional[bool] + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[SequenceNotStr[str]] + """A list of tool names to match against.""" + + +ToolMcpAllowedTools: TypeAlias = Union[SequenceNotStr[str], ToolMcpAllowedToolsLiveMCPToolFilterParam] + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways(TypedDict, total=False): + """A filter that forces approval for matching tools.""" + + read_only: Optional[bool] + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[SequenceNotStr[str]] + """A list of tool names to match against.""" + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever(TypedDict, total=False): + """A filter that skips approval for matching tools.""" + + read_only: Optional[bool] + """Whether to allow only read-only tools for matching entries.""" + + tool_names: Optional[SequenceNotStr[str]] + """A list of tool names to match against.""" + + +class ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam(TypedDict, total=False): + always: Optional[ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamAlways] + """A filter that forces approval for matching tools.""" + + never: Optional[ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParamNever] + """A filter that skips approval for matching tools.""" + + +ToolMcpRequireApproval: TypeAlias = Union[ + ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam, Literal["always", "never"] +] + + class ToolMcp(TypedDict, total=False): type: Required[Literal["mcp"]] + allowed_callers: Optional[List[Literal["direct", "programmatic"]]] + """The tool invocation context(s).""" + + allowed_tools: Optional[ToolMcpAllowedTools] + """A list of MCP tool names that are allowed for this server.""" + + authorization: Optional[str] + """This value is omitted from returned Live session data.""" + + connector_id: Optional[str] + """The ID of a built-in MCP connector. + + This field is deprecated for models released after September 1, 2026. Use + `server_url` to connect to a remote MCP server, or `tunnel_id` to connect to a + local MCP server. + """ + + defer_loading: Optional[bool] + """Whether this MCP tool should be deferred and discovered via tool search.""" + + headers: Optional[Dict[str, str]] + """Optional HTTP headers to send when connecting to the MCP server.""" + + require_approval: Optional[ToolMcpRequireApproval] + """Controls which MCP tools require explicit user approval before execution.""" + + server_description: Optional[str] + """An optional human-readable description of the MCP server.""" + + server_label: str + """A label used to identify the MCP server.""" + + server_url: Optional[str] + """The URL of the MCP server to connect to.""" + + tunnel_id: Optional[str] + """The Secure MCP Tunnel ID to use instead of a direct server URL.""" + class ToolCustom(TypedDict, total=False): type: Required[Literal["custom"]] diff --git a/tests/lib/live/test_mcp_transport.py b/tests/lib/live/test_mcp_transport.py new file mode 100644 index 0000000000..653c2f4f16 --- /dev/null +++ b/tests/lib/live/test_mcp_transport.py @@ -0,0 +1,104 @@ +from __future__ import annotations + +import json + +import httpx2 +import pytest +from websockets.sync.server import ServerConnection + +from openai import OpenAI +from openai.types.live import SessionStartedEvent, SessionUpdatedEvent +from openai.types.live.session_resource import DelegationResponses +from openai.types.live.responses_delegation_config import ( + ToolMcp, + ToolMcpAllowedToolsLiveMCPToolFilterParam, + ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam, +) + +from ..responses.test_websocket_session import script_server + + +def test_mcp_fields_survive_session_event_transport(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("NO_PROXY", "127.0.0.1") + monkeypatch.setenv("no_proxy", "127.0.0.1") + tools = json.loads( + '[{"type":"mcp","server_label":"fixture","server_url":"https://example.test/mcp","server_description":"MCP response fixture","connector_id":"connector_dropbox","tunnel_id":"tunnel_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","allowed_callers":["direct"],"defer_loading":false,"allowed_tools":{"tool_names":["lookup"],"read_only":true},"require_approval":{"always":{"tool_names":["write"],"read_only":false},"never":{"tool_names":["lookup"],"read_only":true}}},{"type":"mcp","server_url":null,"server_description":null,"connector_id":null,"tunnel_id":null,"allowed_callers":null,"defer_loading":null,"allowed_tools":null,"require_approval":null},{"type":"mcp"},{"type":"mcp","allowed_tools":{"tool_names":null,"read_only":null},"require_approval":{"always":null,"never":null}},{"type":"mcp","allowed_tools":{},"require_approval":{}}]' + ) + exercised = [] + + def script(socket: ServerConnection) -> None: + for kind in ("session.started", "session.updated"): + exercised.append(json.loads(socket.recv(timeout=5))["type"]) + socket.send( + json.dumps( + { + "type": kind, + "event_id": "event_fixture", + "session": { + "id": "live_fixture", + "model": "gpt-live-1", + "status": "active", + "expires_at": 123, + "delegation": { + "type": "responses", + "responses": {"model": "model_fixture", "tools": tools}, + }, + }, + } + ) + ) + + with script_server(script) as url: + with OpenAI(api_key="fake-live-key", base_url=url, http_client=httpx2.Client(trust_env=False)) as client: + with client.live.connect() as connection: + for kind in ("session.started", "session.updated"): + if kind == "session.started": + connection.session.start(session={"model": "gpt-live-1"}) + else: + connection.session.update(session={}) + event = connection.recv() + assert isinstance(event, (SessionStartedEvent, SessionUpdatedEvent)) + assert event.type == kind + assert isinstance(event.session.delegation, DelegationResponses) + observed = event.session.delegation.responses.tools + assert observed is not None + tool = observed[0] + assert isinstance(tool, ToolMcp) + assert tool.server_label == "fixture" + assert tool.server_description == "MCP response fixture" + assert tool.server_url == "https://example.test/mcp" + assert tool.connector_id == "connector_dropbox" + assert tool.tunnel_id == "tunnel_" + "a" * 32 + assert tool.allowed_callers == ["direct"] + assert tool.defer_loading is False + assert isinstance(tool.allowed_tools, ToolMcpAllowedToolsLiveMCPToolFilterParam) + assert tool.allowed_tools.tool_names == ["lookup"] + assert tool.allowed_tools.read_only is True + assert isinstance(tool.require_approval, ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam) + assert tool.require_approval.always is not None + assert tool.require_approval.always.tool_names == ["write"] + assert tool.require_approval.always.read_only is False + assert tool.require_approval.never is not None + assert tool.require_approval.never.tool_names == ["lookup"] + assert tool.require_approval.never.read_only is True + for absent in observed[1:3]: + assert isinstance(absent, ToolMcp) + assert absent.server_description is None + assert absent.server_url is None + assert absent.connector_id is None + assert absent.tunnel_id is None + assert absent.allowed_callers is None + assert absent.defer_loading is None + assert absent.allowed_tools is None + assert absent.require_approval is None + for nested in observed[3:]: + assert isinstance(nested, ToolMcp) + assert isinstance(nested.allowed_tools, ToolMcpAllowedToolsLiveMCPToolFilterParam) + assert nested.allowed_tools.tool_names is None + assert nested.allowed_tools.read_only is None + assert isinstance( + nested.require_approval, ToolMcpRequireApprovalLiveMCPRequireApprovalFilterParam + ) + assert nested.require_approval.always is None + assert nested.require_approval.never is None + assert exercised == ["session.start", "session.update"]