ShawnGarello opened a new pull request, #1485:
URL: https://github.com/apache/flink-agents/pull/1485

   Linked issue: Part of #1059
   
   ### Purpose of change
   
   An MCP tool's result now reaches the model as text and media blocks, one per 
MCP content item, in order, in Java and Python. Before, both serialized the 
result into one JSON text block: an image arrived as base64 inside JSON, and a 
text-only result as a JSON array such as `["result"]`. Integrations that take 
media in tool results can now use it.
   
   #### Runtime flow
   
   1. `MCPServer.callTool` (Java) / `call_tool_async` (Python) gets the 
`CallToolResult`. If `isError` is set, it raises with the text items only; 
otherwise it converts each item (`MCPContentExtractor.toContentBlock` / 
`utils.to_content_block`) and returns the blocks.
   2. `MCPTool.call` returns them as `ToolResponse.success(...)`; any exception 
becomes `ToolResponse.error("Error calling MCP tool '<name>': ...")`, as before.
   3. The chat model integration's existing validation accepts or rejects each 
media block.
   
   #### Key decisions
   
   The mapping is the same in both languages:
   
   | MCP item | Block |
   |---|---|
   | `TextContent` | `TextBlock` |
   | `ImageContent` / `AudioContent` | `ImageBlock` / `AudioBlock`, Base64 
source |
   | Embedded text resource | `TextBlock` with its text, no URI |
   | Embedded blob resource | By the media type's essence (before `;`, trimmed, 
lowercased): `image/*` → `ImageBlock`, `audio/*` → `AudioBlock`, `video/*` → 
`VideoBlock`, else `DocumentBlock`; Base64 source |
   | `ResourceLink`, any other item | `TextBlock` holding the item as JSON |
   
   - No fallback: media an integration can't take in tool results raises its 
existing `UnsupportedContentBlockException` / `UnsupportedContentBlockError`, 
as function-tool media does. A text fallback would hide it from the model.
   - Resource links stay text: MCP URIs are often `file://` or custom schemes 
providers can't fetch.
   - A blob without a media type fails rather than guessing one.
   
   ### Behavioral Semantics
   
   #### Interaction decisions
   
   | `isError` | Content | Result |
   |---|---|---|
   | not set | text only | one `TextBlock` per item |
   | not set | with media | blocks in order; the integration decides |
   | not set | a blob without a media type | `ToolResponse.error` |
   | set | anything | `ToolResponse.error` with the text items only |
   
   #### Behavioral contracts
   
   1. Items become blocks per the mapping, in order; media blocks keep 
`mimeType` (with parameters) and the Base64 data unchanged.
   2. A text-only result gives one `TextBlock` per item; the response text is 
the items concatenated, without JSON brackets or quotes.
   3. A `ResourceLink` gives one `TextBlock` whose text is JSON with the link's 
`uri` and `name`.
   4. An `isError` result raises from the server method with `MCP tool '<name>' 
returned an error: ` plus the text items joined by `\n`, and no media or 
resource data. `MCPTool.call` returns it as an error response.
   5. A blob with a null or blank `mimeType` gives the error response `Error 
calling MCP tool '<name>': An embedded MCP blob resource has no media type 
(mimeType).`
   6. A successful call returns `ToolResponse.success` with the blocks and tool 
name; Java also sets the execution time.
   7. MCP media blocks are the types a function tool returns, so a text-only 
integration rejects them with its existing error.
   8. A Python MCP tool used from a Java agent keeps its media blocks across 
the bridge.
   
   #### Failure behavior
   
   - `isError`, or a blob without a media type (`IllegalArgumentException` / 
`ValueError`; one such item fails the whole result): `MCPTool.call` returns an 
error response.
   - Media the integration can't take: its validation raises while building the 
request; nothing is sent.
   
   ### Tests
   
   Java: `MCPToolTest` unless prefixed. Python: `test_mcp_content_blocks.py`.
   
   | Contract | Java | Python |
   |---|---|---|
   | 1, 6 | `callReturnsBlocksInOrder`; `MCPContentExtractorTest`, one test per 
row | `test_tool_call_returns_blocks_in_order`; 
`test_content_item_becomes_block` |
   | 2 | `textOnlyResultIsPlainText` | `test_text_only_result_is_plain_text` |
   | 3 | `MCPContentExtractorTest.resourceLinkBecomesJsonText` | 
`test_resource_link_becomes_json_text` |
   | 4 | `toolErrorLeavesOutMedia`; 
`MCPServerTest.protocolToolErrorIsNotReturnedAsSuccessfulContent` | 
`test_tool_error_reports_text_and_leaves_out_media` |
   | 5 | `blobWithoutMediaTypeFailsTheCall` | 
`test_blob_without_media_type_fails_the_call` |
   | 7 | `imageResultFailsTextOnlyMediaCheck` | 
`test_image_result_fails_text_only_provider` |
   | 8 | `PythonToolResultConverterTest` | — |
   
   Not verified:
   - No chat model is called. Java's contract 7 test calls 
`UnsupportedContentBlockException.rejectMedia`, which the watsonx connection 
calls, as the MCP module has no provider dependency; Python's uses the watsonx 
connection.
   - Contract 8 is tested from the bridge map on; the Pemja call and an agent 
job with a media-returning MCP tool aren't run.
   - Python tests use mcp 1.30; the floor, 1.8.0, which lacks `AudioContent` 
and `ResourceLink`, is untested.
   - Empty media data (rejected by the block constructors) is untested.
   
   <details>
   <summary>Test runs, a local MCP server check and implementation 
invariants</summary>
   
   - `mvn -B test -pl integrations/mcp`: 62 passed (`MCPContentExtractorTest` 
14, `MCPToolTest` 14, `MCPServerTest` 20, the rest unchanged). `mvn -B test -pl 
plan -Dtest=PythonToolResultConverterTest`: 1 passed.
   - Python: the MCP, `ToolResponse` and plan action tests, 380 passed; the 
full unit suite, 3896 passed and 18 skipped.
   - `spotless:check` on `integrations/mcp` and `plan`, `ruff check` and `ruff 
format --check` (0.11.13), and `./tools/check-license.sh` pass.
   - With the main code reverted, all 23 Python tests for contracts 1–7 fail. 
In Java the two updated `MCPServerTest` tests fail, and `MCPToolTest` and 
`MCPContentExtractorTest` don't compile against the old API.
   - Against a local FastMCP server (mcp 1.30), the Python and Java `MCPTool` 
returned the same blocks: an `image/png` image, `audio/wav` audio, a text 
resource as text, a PDF blob as a `document` block, text/image/text in order, 
an error for a blob without a media type, and an error carrying the text of a 
tool that raised.
   - More tests per contract: 2, 
`MCPServerTest.successfulProtocolToolResultReturnsContent` and 
`test_mcp.py::test_mcp_tool_success_returns_text_blocks`; 5, 
`MCPContentExtractorTest.blobWithoutMediaTypeIsRejected` and 
`test_blob_without_media_type_is_rejected`; 1 and 3, 
`test_mcp.py::test_mcp_resource_result_converts_to_tool_response` (a text 
resource, a blob with a media type and a resource link through `MCPTool.call`).
   - The resource-link JSON differs by language: Python's 
`model_dump(mode="json")` includes null fields, and Java's Jackson output omits 
them. Both contain `type`, `uri` and `name`.
   - Neither SDK can build an `EmbeddedResource` whose contents are neither 
text nor blob; such an item would fall through to the JSON row.
   - Python doesn't import `AudioContent` or `ResourceLink` at module level, 
because mcp 1.8.0 lacks them.
   - MCP prompts and `structuredContent` are unchanged.
   
   </details>
   
   ### API
   
   Breaking, before 1.0:
   - Java: `MCPServer.callTool` returns `List<DataContentBlock>` instead of 
strings and maps; `MCPContentExtractor.extractContentItem(Object)` becomes 
`toContentBlock(McpSchema.Content)`.
   - Python: `MCPServer.call_tool_async` returns `list[DataContentBlock]` 
instead of strings and dicts; `utils.extract_mcp_content_item` becomes 
`to_content_block`; `MCPTool.call` returns a `ToolResponse`, not the raw list.
   
   For a caller who changes nothing: the model sees `result`, not `["result"]`; 
media raises with integrations that don't take it in tool results, where it 
used to arrive as JSON text; an `isError` message leaves out non-text items. 
Tool names, schemas, prompts, retries and timeouts are unchanged.
   
   ### Documentation
   
   - [ ] `doc-needed`
   - [ ] `doc-not-needed`
   - [x] `doc-included`
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   - [x] Yes
   - [ ] No
   
   Generated-by: Claude Code 2.1.332 (Claude Opus 5.5)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to