wenjin272 commented on code in PR #1475:
URL: https://github.com/apache/flink-agents/pull/1475#discussion_r4768985433
##########
docs/content/docs/development/create-agent/yaml.md:
##########
@@ -301,8 +301,25 @@ mcp_servers:
clazz: mcp
type: python
endpoint: http://127.0.0.1:8000/mcp
+
+ # Bundled Java MCP server (requires JDK 17+ at execution time).
+ - name: secured_java_mcp
+ clazz: mcp
+ type: java
+ endpoint: https://mcp.example.com/mcp
+ headers:
+ X-Client: flink-agents
+ timeout: 30s
+ auth:
+ authType: bearer
+ token: ${MCP_TOKEN}
Review Comment:
Neither YAML loader expands environment variables. I verified that even with
`MCP_TOKEN` set, the descriptor retains the literal string `${MCP_TOKEN}`,
which would be sent as the bearer token.
Could we use an explicit example value here, or clarify that the application
must substitute the environment variable before loading the YAML?
##########
python/flink_agents/plan/agent_plan.py:
##########
@@ -561,10 +563,25 @@ def _add_mcp_server(
The server is not contacted here. Its tools and prompts are discovered when
the operator starts, see ``flink_agents.runtime.mcp_resource_discovery``.
+
+ A Java declaration is carried by a ``JavaResourceProvider`` for the Java
+ runtime, which owns its discovery; the Python runtime never provides it.
+ The ``mcp`` alias resolves to a declaration placeholder, and the default
+ implementation is named here, mirroring the Java plan.
"""
- resource_providers.append(
- PythonResourceProvider.get(name=name, descriptor=descriptor)
- )
+ if descriptor.language == "java":
+ if descriptor.target_clazz == ResourceName.JAVA_MCP_SERVER_PLACEHOLDER:
+ descriptor = JavaResourceDescriptor(
+ clazz=ResourceName.JAVA_MCP_SERVER,
+ **descriptor.arguments,
+ )
+ resource_providers.append(
+ JavaResourceProvider.get(name, descriptor, ResourceType.MCP_SERVER)
+ )
Review Comment:
Could we also complete Python-side access to the Java MCP server’s
discovered tools and prompts in this PR?
Both YAML loaders should interpret `type: java` consistently, and Python
actions and chat models should be able to use those resources. Currently,
discovery registers them only in the Java cache, while Python resource lookup
does not consult that cache.
The new Python-entry E2E uses a Java action, so it does not cover this path.
Could we add the bridge support and an E2E with a Python action consuming a
Java MCP tool and prompt?
##########
python/flink_agents/api/resource.py:
##########
@@ -499,3 +499,9 @@ class RerankModel:
# MCP resource names
MCP_SERVER = "flink_agents.integrations.mcp.mcp.MCPServer"
+
+ # Java-side names behind the YAML `mcp` alias: the declaration
placeholder, as in
+ # the Java API (`ResourceName.MCP_SERVER`), plus the default
implementation that
+ # the plan names when it builds the agent plan.
+ JAVA_MCP_SERVER_PLACEHOLDER = "MCP_SERVER_PLACEHOLDER"
+ JAVA_MCP_SERVER = "org.apache.flink.agents.integrations.mcp.MCPServer"
Review Comment:
Could we use a single `ResourceName.MCP_SERVER` placeholder in both Java and
Python, and let the plan resolve it based on the declared language?
We have one built-in MCP implementation per language, so users only need to
choose Java or Python. The implementation class names can stay internal,
without exposing separate `JAVA_MCP_SERVER_PLACEHOLDER` and `JAVA_MCP_SERVER`
constants. Custom implementations could still be declared using a fully
qualified class name.
##########
runtime/src/test/java/org/apache/flink/agents/runtime/JavaMCPResourceDiscoveryJdkTest.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.flink.agents.runtime;
+
+import org.apache.flink.agents.api.resource.Resource;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.resource.ResourceName;
+import org.apache.flink.agents.api.resource.ResourceType;
+import org.apache.flink.agents.plan.resourceprovider.JavaResourceProvider;
+import org.apache.flink.agents.plan.resourceprovider.ResourceProvider;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests the JDK 17+ execution guard without depending on the bundled MCP
class. */
+class JavaMCPResourceDiscoveryJdkTest {
+
+ private static ResourceCache cache(JavaResourceProvider provider) {
+ Map<ResourceType, Map<String, ResourceProvider>> providers = new
HashMap<>();
+ Map<String, ResourceProvider> servers = new HashMap<>();
+ servers.put("bundled", provider);
+ providers.put(ResourceType.MCP_SERVER, servers);
+ return new ResourceCache(providers);
+ }
+
+ @Test
+ void bundledJavaMCPRequiresJava17BeforeClassLoading() {
+ ResourceDescriptor descriptor =
+
ResourceDescriptor.Builder.newBuilder(ResourceName.JAVA_MCP_SERVER)
+ .addInitialArgument("endpoint",
"http://127.0.0.1:8000/mcp")
+ .build();
+ ResourceCache cache =
+ cache(new JavaResourceProvider("bundled",
ResourceType.MCP_SERVER, descriptor));
+
+ // On JDK 17+ discovery may proceed to connect; below 17 it must fail
before class
+ // loading, even when the Python-built plan named the bundled
implementation.
+ assertThatThrownBy(
+ () ->
+
JavaMCPResourceDiscovery.discoverJavaMCPResources(
+ cache.getProviders(), cache))
+ .isInstanceOf(
+ Runtime.version().feature() < 17
+ ? UnsupportedOperationException.class
+ : IllegalStateException.class)
Review Comment:
On JDK 17+, this test connects to `localhost:8000` and expects discovery to
fail. I reproduced a failure by starting the repository’s MCP test server:
discovery succeeds, and the assertion fails with “Expecting code to raise a
throwable.”
Could we make this test independent of a real endpoint? For example, test
the rejection on JDK < 17 and use a stub provider to verify that the guard
allows discovery to proceed on JDK 17+.
##########
runtime/src/main/java/org/apache/flink/agents/runtime/ResourceCache.java:
##########
@@ -240,6 +240,11 @@ public ResourceContextImpl getResourceContext() {
return resourceContext;
}
+ /** Providers carried by this cache, without inherited parent providers. */
+ Map<ResourceType, Map<String, ResourceProvider>> getProviders() {
+ return resourceProviders;
+ }
Review Comment:
Could we call the existing `cache.discoverMCPResources()` in these tests
instead of adding `getProviders()`? It invokes the same discovery logic, so we
can test the JDK checks without exposing the internal provider map.
##########
runtime/src/main/java/org/apache/flink/agents/runtime/JavaMCPResourceDiscovery.java:
##########
@@ -83,6 +85,18 @@ private static void discoverServer(
Map<String, String> toolOwners,
Map<String, String> promptOwners)
throws Exception {
+ if (provider instanceof JavaResourceProvider) {
+ ResourceDescriptor descriptor = ((JavaResourceProvider)
provider).getDescriptor();
+ if (ResourceName.JAVA_MCP_SERVER.equals(descriptor.getClazz())
+ && Runtime.version().feature() < 17) {
+ throw new UnsupportedOperationException(
+ "MCP server '"
+ + serverName
+ + "' uses the bundled Java MCP implementation,
which requires"
+ + " JDK 17+. Run this job on JDK 17+ or
declare a Python MCP"
+ + " server.");
Review Comment:
Could we use “built-in Java MCP server” instead of “bundled Java MCP
implementation” here? For example:
```text
The built-in Java MCP server '%s' requires JDK 17+. Run this job on JDK 17+
or declare a Python MCP server.
```
--
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]