Skip to content

Commit a4142fa

Browse files
committed
Add support for Remote MCP Tool
1 parent ffaafce commit a4142fa

19 files changed

+822
-20
lines changed

src/Responses/Responses/CreateResponse.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
1313
use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall;
1414
use OpenAI\Responses\Responses\Output\OutputFunctionToolCall;
15+
use OpenAI\Responses\Responses\Output\OutputMcpApprovalRequest;
16+
use OpenAI\Responses\Responses\Output\OutputMcpCall;
17+
use OpenAI\Responses\Responses\Output\OutputMcpListTools;
1518
use OpenAI\Responses\Responses\Output\OutputMessage;
1619
use OpenAI\Responses\Responses\Output\OutputMessageContentOutputText;
1720
use OpenAI\Responses\Responses\Output\OutputReasoning;
@@ -20,6 +23,7 @@
2023
use OpenAI\Responses\Responses\Tool\FileSearchTool;
2124
use OpenAI\Responses\Responses\Tool\FunctionTool;
2225
use OpenAI\Responses\Responses\Tool\ImageGenerationTool;
26+
use OpenAI\Responses\Responses\Tool\RemoteMcpTool;
2327
use OpenAI\Responses\Responses\Tool\WebSearchTool;
2428
use OpenAI\Responses\Responses\ToolChoice\FunctionToolChoice;
2529
use OpenAI\Responses\Responses\ToolChoice\HostedToolChoice;
@@ -33,9 +37,13 @@
3337
* @phpstan-import-type OutputMessageType from OutputMessage
3438
* @phpstan-import-type OutputReasoningType from OutputReasoning
3539
* @phpstan-import-type OutputWebSearchToolCallType from OutputWebSearchToolCall
40+
* @phpstan-import-type OutputMcpListToolsType from OutputMcpListTools
41+
* @phpstan-import-type OutputMcpApprovalRequestType from OutputMcpApprovalRequest
42+
* @phpstan-import-type OutputMcpCallType from OutputMcpCall
3643
* @phpstan-import-type ComputerUseToolType from ComputerUseTool
3744
* @phpstan-import-type FileSearchToolType from FileSearchTool
3845
* @phpstan-import-type ImageGenerationToolType from ImageGenerationTool
46+
* @phpstan-import-type RemoteMcpToolType from RemoteMcpTool
3947
* @phpstan-import-type FunctionToolType from FunctionTool
4048
* @phpstan-import-type WebSearchToolType from WebSearchTool
4149
* @phpstan-import-type ErrorType from CreateResponseError
@@ -46,8 +54,8 @@
4654
* @phpstan-import-type ReasoningType from CreateResponseReasoning
4755
*
4856
* @phpstan-type ToolChoiceType 'none'|'auto'|'required'|FunctionToolChoiceType|HostedToolChoiceType
49-
* @phpstan-type ToolsType array<int, ComputerUseToolType|FileSearchToolType|FunctionToolType|WebSearchToolType|ImageGenerationToolType>
50-
* @phpstan-type OutputType array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType>
57+
* @phpstan-type ToolsType array<int, ComputerUseToolType|FileSearchToolType|FunctionToolType|WebSearchToolType|ImageGenerationToolType|RemoteMcpToolType>
58+
* @phpstan-type OutputType array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType>
5159
* @phpstan-type CreateResponseType array{id: string, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details: IncompleteDetailsType|null, instructions: string|null, max_output_tokens: int|null, model: string, output: OutputType, output_text: string|null, parallel_tool_calls: bool, previous_response_id: string|null, reasoning: ReasoningType|null, store: bool, temperature: float|null, text: ResponseFormatType, tool_choice: ToolChoiceType, tools: ToolsType, top_p: float|null, truncation: 'auto'|'disabled'|null, usage: UsageType|null, user: string|null, metadata: array<string, string>|null}
5260
*
5361
* @implements ResponseContract<CreateResponseType>
@@ -65,8 +73,8 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati
6573
/**
6674
* @param 'response' $object
6775
* @param 'completed'|'failed'|'in_progress'|'incomplete' $status
68-
* @param array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning> $output
69-
* @param array<int, ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool> $tools
76+
* @param array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall> $output
77+
* @param array<int, ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool> $tools
7078
* @param 'auto'|'disabled'|null $truncation
7179
* @param array<string, string> $metadata
7280
*/
@@ -104,13 +112,16 @@ private function __construct(
104112
public static function from(array $attributes, MetaInformation $meta): self
105113
{
106114
$output = array_map(
107-
fn (array $output): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning => match ($output['type']) {
115+
fn (array $output): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall => match ($output['type']) {
108116
'message' => OutputMessage::from($output),
109117
'file_search_call' => OutputFileSearchToolCall::from($output),
110118
'function_call' => OutputFunctionToolCall::from($output),
111119
'web_search_call' => OutputWebSearchToolCall::from($output),
112120
'computer_call' => OutputComputerToolCall::from($output),
113121
'reasoning' => OutputReasoning::from($output),
122+
'mcp_list_tools' => OutputMcpListTools::from($output),
123+
'mcp_approval_request' => OutputMcpApprovalRequest::from($output),
124+
'mcp_call' => OutputMcpCall::from($output),
114125
},
115126
$attributes['output'],
116127
);
@@ -123,12 +134,13 @@ public static function from(array $attributes, MetaInformation $meta): self
123134
: $attributes['tool_choice'];
124135

125136
$tools = array_map(
126-
fn (array $tool): ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool => match ($tool['type']) {
137+
fn (array $tool): ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool => match ($tool['type']) {
127138
'file_search' => FileSearchTool::from($tool),
128139
'web_search_preview', 'web_search_preview_2025_03_11' => WebSearchTool::from($tool),
129140
'function' => FunctionTool::from($tool),
130141
'computer_use_preview' => ComputerUseTool::from($tool),
131142
'image_generation' => ImageGenerationTool::from($tool),
143+
'mcp' => RemoteMcpTool::from($tool),
132144
},
133145
$attributes['tools'],
134146
);
@@ -201,7 +213,7 @@ public function toArray(): array
201213
'metadata' => $this->metadata ?? [],
202214
'model' => $this->model,
203215
'output' => array_map(
204-
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning $output): array => $output->toArray(),
216+
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall $output): array => $output->toArray(),
205217
$this->output
206218
),
207219
'parallel_tool_calls' => $this->parallelToolCalls,
@@ -214,7 +226,7 @@ public function toArray(): array
214226
? $this->toolChoice
215227
: $this->toolChoice->toArray(),
216228
'tools' => array_map(
217-
fn (ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool $tool): array => $tool->toArray(),
229+
fn (ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool $tool): array => $tool->toArray(),
218230
$this->tools
219231
),
220232
'top_p' => $this->topP,

src/Responses/Responses/CreateStreamedResponse.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use OpenAI\Responses\Responses\Streaming\FileSearchCall;
1313
use OpenAI\Responses\Responses\Streaming\FunctionCallArgumentsDelta;
1414
use OpenAI\Responses\Responses\Streaming\FunctionCallArgumentsDone;
15+
use OpenAI\Responses\Responses\Streaming\McpCall;
16+
use OpenAI\Responses\Responses\Streaming\McpCallArgumentsDelta;
17+
use OpenAI\Responses\Responses\Streaming\McpCallArgumentsDone;
18+
use OpenAI\Responses\Responses\Streaming\McpListTools;
1519
use OpenAI\Responses\Responses\Streaming\OutputItem;
1620
use OpenAI\Responses\Responses\Streaming\OutputTextAnnotationAdded;
1721
use OpenAI\Responses\Responses\Streaming\OutputTextDelta;
@@ -40,7 +44,7 @@ final class CreateStreamedResponse implements ResponseContract
4044

4145
private function __construct(
4246
public readonly string $event,
43-
public readonly CreateResponse|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|Error $response,
47+
public readonly CreateResponse|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|McpListTools|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|Error $response,
4448
) {}
4549

4650
/**
@@ -79,6 +83,16 @@ public static function from(array $attributes): self
7983
'response.reasoning_summary_part.done' => ReasoningSummaryPart::from($attributes, $meta), // @phpstan-ignore-line
8084
'response.reasoning_summary_text.delta' => ReasoningSummaryTextDelta::from($attributes, $meta), // @phpstan-ignore-line
8185
'response.reasoning_summary_text.done' => ReasoningSummaryTextDone::from($attributes, $meta), // @phpstan-ignore-line
86+
'response.mcp_list_tools.in_progress',
87+
'response.mcp_list_tools.failed',
88+
'response.mcp_list_tools.completed' => McpListTools::from($attributes, $meta), // @phpstan-ignore-line
89+
'response.mcp_call.in_progress',
90+
'response.mcp_call.failed',
91+
'response.mcp_call.completed' => McpCall::from($attributes, $meta), // @phpstan-ignore-line
92+
'response.mcp_call.arguments.delta',
93+
'response.mcp_call_arguments.delta' => McpCallArgumentsDelta::from($attributes, $meta), // @phpstan-ignore-line
94+
'response.mcp_call.arguments.done',
95+
'response.mcp_call_arguments.done' => McpCallArgumentsDone::from($attributes, $meta), // @phpstan-ignore-line
8296
'error' => Error::from($attributes, $meta), // @phpstan-ignore-line
8397
default => throw new UnknownEventException('Unknown Responses streaming event: '.$event),
8498
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Responses\Output;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Responses\Concerns\ArrayAccessible;
9+
use OpenAI\Testing\Responses\Concerns\Fakeable;
10+
11+
/**
12+
* @phpstan-type OutputMcpApprovalRequestType array{id: string, server_label: string, name: string, arguments: string, type: 'mcp_approval_request'}
13+
*
14+
* @implements ResponseContract<OutputMcpApprovalRequestType>
15+
*/
16+
final class OutputMcpApprovalRequest implements ResponseContract
17+
{
18+
/**
19+
* @use ArrayAccessible<OutputMcpApprovalRequestType>
20+
*/
21+
use ArrayAccessible;
22+
23+
use Fakeable;
24+
25+
/**
26+
* @param 'mcp_approval_request' $type
27+
*/
28+
private function __construct(
29+
public readonly string $id,
30+
public readonly string $serverLabel,
31+
public readonly string $name,
32+
public readonly string $arguments,
33+
public readonly string $type,
34+
) {}
35+
36+
/**
37+
* @param OutputMcpApprovalRequestType $attributes
38+
*/
39+
public static function from(array $attributes): self
40+
{
41+
return new self(
42+
id: $attributes['id'],
43+
serverLabel: $attributes['server_label'],
44+
name: $attributes['name'],
45+
arguments: $attributes['arguments'],
46+
type: $attributes['type'],
47+
);
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
public function toArray(): array
54+
{
55+
return [
56+
'id' => $this->id,
57+
'server_label' => $this->serverLabel,
58+
'type' => $this->type,
59+
'arguments' => $this->arguments,
60+
'name' => $this->name,
61+
];
62+
}
63+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Responses\Output;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Responses\Concerns\ArrayAccessible;
9+
use OpenAI\Testing\Responses\Concerns\Fakeable;
10+
11+
/**
12+
* @phpstan-type OutputMcpCallType array{id: string, server_label: string, type: 'mcp_call', approval_request_id: ?string, arguments: string, error: ?string, name: string, output: ?string}
13+
*
14+
* @implements ResponseContract<OutputMcpCallType>
15+
*/
16+
final class OutputMcpCall implements ResponseContract
17+
{
18+
/**
19+
* @use ArrayAccessible<OutputMcpCallType>
20+
*/
21+
use ArrayAccessible;
22+
23+
use Fakeable;
24+
25+
/**
26+
* @param 'mcp_call' $type
27+
*/
28+
private function __construct(
29+
public readonly string $id,
30+
public readonly string $serverLabel,
31+
public readonly string $type,
32+
public readonly string $arguments,
33+
public readonly string $name,
34+
public readonly ?string $approvalRequestId = null,
35+
public readonly ?string $error = null,
36+
public readonly ?string $output = null,
37+
38+
) {}
39+
40+
/**
41+
* @param OutputMcpCallType $attributes
42+
*/
43+
public static function from(array $attributes): self
44+
{
45+
return new self(
46+
id: $attributes['id'],
47+
serverLabel: $attributes['server_label'],
48+
type: $attributes['type'],
49+
arguments: $attributes['arguments'],
50+
name: $attributes['name'],
51+
approvalRequestId: $attributes['approval_request_id'],
52+
error: $attributes['error'],
53+
output: $attributes['output'],
54+
);
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public function toArray(): array
61+
{
62+
return [
63+
'id' => $this->id,
64+
'server_label' => $this->serverLabel,
65+
'type' => $this->type,
66+
'arguments' => $this->arguments,
67+
'name' => $this->name,
68+
'approval_request_id' => $this->approvalRequestId,
69+
'error' => $this->error,
70+
'output' => $this->output,
71+
];
72+
}
73+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Responses\Output;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Responses\Concerns\ArrayAccessible;
9+
use OpenAI\Testing\Responses\Concerns\Fakeable;
10+
11+
/**
12+
* @phpstan-import-type OutputMcpListToolsToolType from OutputMcpListToolsTool
13+
*
14+
* @phpstan-type OutputMcpListToolsType array{id: string, server_label: string, type: 'mcp_list_tools', tools: OutputMcpListToolsToolType[]}
15+
*
16+
* @implements ResponseContract<OutputMcpListToolsType>
17+
*/
18+
final class OutputMcpListTools implements ResponseContract
19+
{
20+
/**
21+
* @use ArrayAccessible<OutputMcpListToolsType>
22+
*/
23+
use ArrayAccessible;
24+
25+
use Fakeable;
26+
27+
/**
28+
* @param 'mcp_list_tools' $type
29+
* @param OutputMcpListToolsTool[] $tools
30+
*/
31+
private function __construct(
32+
public readonly string $id,
33+
public readonly string $serverLabel,
34+
public readonly string $type,
35+
public readonly array $tools,
36+
) {}
37+
38+
/**
39+
* @param OutputMcpListToolsType $attributes
40+
*/
41+
public static function from(array $attributes): self
42+
{
43+
$tools = array_map(
44+
fn (array $result): OutputMcpListToolsTool => OutputMcpListToolsTool::from($result),
45+
$attributes['tools']
46+
);
47+
48+
return new self(
49+
id: $attributes['id'],
50+
serverLabel: $attributes['server_label'],
51+
type: $attributes['type'],
52+
tools: $tools,
53+
);
54+
}
55+
56+
/**
57+
* {@inheritDoc}
58+
*/
59+
public function toArray(): array
60+
{
61+
return [
62+
'id' => $this->id,
63+
'server_label' => $this->serverLabel,
64+
'type' => $this->type,
65+
'tools' => array_map(
66+
fn (OutputMcpListToolsTool $tool) => $tool->toArray(),
67+
$this->tools
68+
),
69+
];
70+
}
71+
}

0 commit comments

Comments
 (0)