diff --git a/includes/Handlers/Tools/ToolsHandler.php b/includes/Handlers/Tools/ToolsHandler.php index 09cca42..b929655 100644 --- a/includes/Handlers/Tools/ToolsHandler.php +++ b/includes/Handlers/Tools/ToolsHandler.php @@ -133,6 +133,15 @@ public function call_tool( array $message, int $request_id = 0 ): array { ), ); + // Extract and store metadata before adding result to response content. + $response['_metadata'] = $result['_metadata'] ?? array( + 'component_type' => 'tool', + 'tool_name' => $request_params['name'], + ); + + // Remove metadata from result so it doesn't appear in content or structuredContent. + unset( $result['_metadata'] ); + // @todo: add support for EmbeddedResource schema.ts:619. if ( isset( $result['type'] ) && 'image' === $result['type'] ) { $response['content'][0]['type'] = 'image'; @@ -145,12 +154,6 @@ public function call_tool( array $message, int $request_id = 0 ): array { $response['structuredContent'] = $result; } - // Add metadata from result if present, or create basic metadata. - $response['_metadata'] = $result['_metadata'] ?? array( - 'component_type' => 'tool', - 'tool_name' => $request_params['name'], - ); - return $response; } catch ( \Throwable $exception ) { $this->mcp->error_handler->log(