mjolnir-strike opened a new pull request, #1468:
URL: https://github.com/apache/flink-agents/pull/1468

   Linked issue: #1465, #1461
   
   ### Purpose of change
   
   A Python `MCPServer`'s `auth` is now a typed configuration stored in the 
plan as plain JSON:
   - `BasicAuth`, `BearerTokenAuth` and `ApiKeyAuth` use the JSON shape of the 
Java classes (`authType`, `username`/`password`, `token`, 
`headerName`/`apiKey`).
   - `CustomAuth` names a factory (`<module>:<qualname>`) and JSON keyword 
arguments, for OAuth or any other `httpx.Auth`.
   
   Before, `auth` was a cloudpickled `httpx.Auth`, and any string value was 
unpickled. Connections now use `streamable_http_client` instead of the 
deprecated `streamablehttp_client` (`mcp>=1.24.0`). Both touch the same lines: 
the new transport takes the client that carries the auth.
   
   #### Runtime flow
   
   1. Descriptor arguments are stored as JSON in the plan.
   2. At operator start, `MCPServer(**arguments)` rejects a string or 
`httpx.Auth` (`mode="before"` validator), then picks the model by `authType`.
   3. Each connection builds an `httpx.AsyncClient` (`headers`, 
`Timeout(timeout, read=sse_read_timeout)`, the cached `httpx.Auth`, 
`follow_redirects=True`), opens `streamable_http_client(endpoint, 
http_client=client)` and `ClientSession` on it, and closes the client last.
   
   #### Key decisions
   
   - **A public `httpx.AsyncClient` with `follow_redirects=True`, not the SDK's 
private `create_mcp_http_client`.** The old wrapper follows redirects on 
1.24–1.29; 1.30 ignores the flag.
   - **snake_case attributes with the Java names as aliases.** Dumps use the 
aliases, so the JSON matches Java's. Input also accepts snake_case, for Python 
construction.
   - **One `httpx.Auth` per server**: each tool call connects anew, and 
rebuilding would lose OAuth tokens.
   - **`CustomAuth` calls the code that config names**, with the same trust as 
a resource's `clazz`. Nothing is unpickled. A class plus constructor arguments 
was rejected: OAuth providers need storage and callbacks that only code can 
supply.
   - **Strings and `httpx.Auth` instances raise `TypeError`.** An instance 
cannot be stored as JSON, and pydantic passes a `TypeError` through unwrapped, 
so a credential in the value never reaches the message.
   
   ### Behavioral Semantics
   
   #### Interaction decisions
   
   | `headers` | `auth` | Sent on each request |
   |---|---|---|
   | set | none | `headers` only, no `Authorization` |
   | set | typed, different name | both |
   | set | typed, same name | the auth value (Java's `applyAuth` adds a second 
value) |
   | any | string or `httpx.Auth` | rejected when the server is built |
   
   #### Behavioral contracts
   
   1. Typed auth dumps to `authType` plus the Java field names and loads back 
equal.
   2. A YAML mapping or a descriptor model reaches `MCPServer` through the plan 
JSON unchanged.
   3. Every request carries `Basic base64(UTF-8 user:pass)`, `Bearer <token>` 
or `<headerName>: <apiKey>`, plus `headers`.
   4. A string `auth` raises `TypeError` without repeating the value, and is 
never decoded or unpickled.
   5. An `httpx.Auth` instance raises `TypeError` naming its type.
   6. These raise a `ValidationError` when the server is built: unknown or 
missing `authType`, a missing or extra field, a malformed factory path, or 
non-JSON `arguments`.
   7. Each server instance calls `factory(**arguments)` once, on first 
connection, and reuses the result.
   8. `repr()` hides passwords, tokens, API keys and `CustomAuth` arguments (as 
Java's `toString` does).
   9. A same-origin 307 redirect is followed with auth.
   10. No `DeprecationWarning` on connect.
   11. Each connection's client has the configured timeouts and is closed 
afterwards.
   
   #### Failure behavior
   
   - Invalid config (4–6) raises at operator start, not at plan building. An 
`httpx.Auth` in a descriptor still fails plan serialization, as on `main`.
   - Factory import errors, and a factory that returns a non-`httpx.Auth` 
(`TypeError`), are raised on first connection. Startup discovery reports them 
as its `RuntimeError` naming the server.
   - On mcp 1.24–1.29 a cross-origin redirect keeps an `ApiKeyAuth` header, 
like a `headers` entry on `main`; 1.30 refuses it.
   
   ### Tests
   
   In `integrations/mcp/tests/test_mcp.py` (`test_` prefix dropped):
   
   | Contract | Tests |
   |---|---|
   | 1 | `auth_serializes_as_plain_json` |
   | 2 | `auth_from_yaml_…`, `auth_declared_in_a_descriptor_…` |
   | 3 | `auth_and_headers_are_sent_on_every_request`, 
`without_auth_only_the_configured_headers_are_sent`, 
`auth_replaces_a_header_with_the_same_name` |
   | 4 | `string_auth_is_rejected_and_never_unpickled`, 
`string_auth_in_a_descriptor_…` |
   | 5 | `httpx_auth_instance_is_rejected` |
   | 6 | `invalid_auth_config_is_rejected` |
   | 7 | `custom_auth_is_built_once_…` |
   | 8 | `auth_repr_hides_the_secret` |
   | 9 | `same_origin_redirect_is_followed_with_auth` |
   | 10 | `without_auth_only_…` |
   | 11 | `each_connection_uses_a_client_…` |
   | Failures | `custom_auth_factory_error…` (2), 
`string_auth_in_a_descriptor_…` |
   
   By risk: auth headers, redirects and client lifecycle (3, 7, 9–11) run 
through the real SDK transport against a local server; JSON shape and 
validation (1, 2, 4–6, 8) are unit-level. 
`custom_auth_builds_an_oauth_provider_from_json` replaces the cloudpickle test.
   
   **Not verified:**
   - OAuth against a real authorization server.
   - Java-loaded YAML declaring a Python server with `auth`.
   - The JDK < 17 fallback that runs a Java `@MCPServer` as this Python server. 
A Java `Auth` there may now load.
   - mcp 1.25–1.28.
   
   <details><summary>Evidence: runs and regression checks</summary>
   
   - `integrations/mcp` and `runtime/tests/test_mcp_resource_discovery.py` pass 
on Python 3.10, 3.11 and 3.12 with mcp 1.30.0, and on 3.12 with mcp 1.24.0 and 
1.29.0. `ruff check` is clean.
   - On the old `mcp.py`, all 36 new tests fail. The string test's payload is 
unpickled and run there.
   - The redirect test fails on mcp 1.24 and 1.29 without 
`follow_redirects=True`.
   - The client test fails if the client is not closed, or is built without the 
timeouts.
   
   </details>
   
   ### API
   
   - **Breaking:** `auth` no longer accepts an `httpx.Auth` instance or a 
base64 string. Plans saved with pickled auth must declare it again.
   - **New:** `flink_agents.integrations.mcp.auth`, with a closed base 
`MCPAuth`.
   - **Dependency:** `mcp>=1.24.0,<2` (was `>=1.8.0`).
   - **Unchanged:** servers without `auth`, and Java code. A Java server still 
cannot read an `auth` mapping from YAML or from a Python descriptor.
   
   ### Documentation
   
   - [ ] `doc-needed` <!-- Your PR changes impact docs -->
   - [ ] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-included` <!-- Your PR already contains the necessary 
documentation updates -->
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   - [ ] Yes
   - [ ] No
   
   Generated-by: Claude Code 2.1.330 (Claude Opus 5.5)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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