6
6
7
7
use OpenAI \Contracts \ResponseContract ;
8
8
use OpenAI \Responses \Concerns \ArrayAccessible ;
9
+ use OpenAI \Responses \Responses \GenericResponseError ;
9
10
use OpenAI \Testing \Responses \Concerns \Fakeable ;
10
11
11
12
/**
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
+ * @phpstan-import-type ErrorType from GenericResponseError
14
+ *
15
+ * @phpstan-type OutputMcpCallType array{id: string, server_label: string, type: 'mcp_call', approval_request_id: ?string, arguments: string, error: string|ErrorType|null, name: string, output: ?string}
13
16
*
14
17
* @implements ResponseContract<OutputMcpCallType>
15
18
*/
@@ -32,7 +35,7 @@ private function __construct(
32
35
public readonly string $ arguments ,
33
36
public readonly string $ name ,
34
37
public readonly ?string $ approvalRequestId = null ,
35
- public readonly ?string $ error = null ,
38
+ public readonly ?GenericResponseError $ error = null ,
36
39
public readonly ?string $ output = null ,
37
40
) {}
38
41
@@ -41,14 +44,28 @@ private function __construct(
41
44
*/
42
45
public static function from (array $ attributes ): self
43
46
{
47
+ // OpenAI has odd structure (presumably a bug) where the errorType can sometimes be a full-fledged HTTP error object.
48
+ // As MCP calls are valid HTTP requests - we need to handle strings & objects here.
49
+ $ errorType = null ;
50
+ if (isset ($ attributes ['error ' ])) {
51
+ if (is_array ($ attributes ['error ' ])) {
52
+ $ errorType = GenericResponseError::from ($ attributes ['error ' ]);
53
+ } elseif (is_string ($ attributes ['error ' ])) {
54
+ $ errorType = GenericResponseError::from ([
55
+ 'code ' => 'unknown_error ' ,
56
+ 'message ' => $ attributes ['error ' ],
57
+ ]);
58
+ }
59
+ }
60
+
44
61
return new self (
45
62
id: $ attributes ['id ' ],
46
63
serverLabel: $ attributes ['server_label ' ],
47
64
type: $ attributes ['type ' ],
48
65
arguments: $ attributes ['arguments ' ],
49
66
name: $ attributes ['name ' ],
50
67
approvalRequestId: $ attributes ['approval_request_id ' ],
51
- error: $ attributes [ ' error ' ] ,
68
+ error: $ errorType ,
52
69
output: $ attributes ['output ' ],
53
70
);
54
71
}
@@ -65,7 +82,9 @@ public function toArray(): array
65
82
'arguments ' => $ this ->arguments ,
66
83
'name ' => $ this ->name ,
67
84
'approval_request_id ' => $ this ->approvalRequestId ,
68
- 'error ' => $ this ->error ,
85
+ 'error ' => $ this ->error instanceof GenericResponseError
86
+ ? $ this ->error ->toArray ()
87
+ : $ this ->error ,
69
88
'output ' => $ this ->output ,
70
89
];
71
90
}
0 commit comments