Skip to content

Commit 15e0757

Browse files
wenhaozhaowenhaozhao
authored andcommitted
feat: Add mcp-tools-java.md to Introduce How to Configure MCP-Tools in ADK-Java
1 parent 183ba55 commit 15e0757

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

docs/tools/mcp-tools.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ The following examples demonstrate how to use `MCPToolset` within the `adk web`
4848

4949
This Python example demonstrates connecting to a local MCP server that provides file system operations.
5050

51+
=== "Python"
52+
5153
#### Step 1: Define your Agent with `MCPToolset`
5254

5355
Create an `agent.py` file (e.g., in `./adk_agent_samples/mcp_agent/agent.py`). The `MCPToolset` is instantiated directly within the `tools` list of your `LlmAgent`.
@@ -108,6 +110,58 @@ Ensure you have an `__init__.py` in the same directory as `agent.py` to make it
108110
from . import agent
109111
```
110112

113+
=== "Java"
114+
115+
116+
Create an `McpToolsExample.java` file. The `MCPToolset` is instantiated directly within the `tools` list of your `LlmAgent`.
117+
118+
* **Important:** Replace `"/path/to/your/folder"` in the `args` list with the **absolute path** to an actual folder on your local system that the MCP server can access.
119+
120+
```java
121+
package com.google.adk.examples;
122+
123+
import com.google.adk.agents.LlmAgent;
124+
import com.google.adk.tools.ToolPredicate;
125+
import com.google.adk.tools.mcp.McpToolset;
126+
import io.modelcontextprotocol.client.transport.ServerParameters;
127+
128+
import java.util.Optional;
129+
130+
public class McpToolsExample {
131+
132+
/**
133+
* IMPORTANT: This MUST be an ABSOLUTE path to a folder the
134+
* npx process can access.
135+
* Replace with a valid absolute path on your system.
136+
* For example: "/Users/youruser/accessible_mcp_files"
137+
* or use a dynamically constructed absolute path:
138+
*/
139+
private static final String TARGET_FOLDER_PATH = "/path/to/your/folder";
140+
141+
public static void main(String[] args) {
142+
ServerParameters serverParameters = ServerParameters.builder("npx")
143+
.args(
144+
"-y", // Argument for npx to auto-confirm install
145+
"@modelcontextprotocol/server-filesystem",
146+
TARGET_FOLDER_PATH
147+
)
148+
.build();
149+
var tools = new McpToolset(
150+
serverParameters,
151+
// Optional: Filter which tools from the MCP server are exposed
152+
Optional.of((ToolPredicate) (tool, readonlyContext) -> true)
153+
);
154+
LlmAgent llmAgent = LlmAgent.builder()
155+
.name("filesystem_assistant_agent")
156+
.description("Help the user manage their files. You can list files, read files, etc.")
157+
.model("gemini-2.5-flash")
158+
.tools(tools)
159+
.build();
160+
}
161+
}
162+
163+
```
164+
111165
#### Step 3: Run `adk web` and Interact
112166

113167
Navigate to the parent directory of `mcp_agent` (e.g., `adk_agent_samples`) in your terminal and run:
@@ -339,6 +393,53 @@ You should see the agent use the Google Maps MCP tools to provide directions or
339393

340394
<img src="../../assets/adk-tool-mcp-maps-adk-web-demo.png" alt="MCP with ADK Web - Google Maps Example">
341395

396+
### Example 3: SSE MCP Server
397+
398+
=== "Java"
399+
400+
401+
This example demonstrates connecting to the SSE MCP server.
402+
403+
Create an `McpToolsExample.java` file. The `MCPToolset` is instantiated directly within the `tools` list of your `LlmAgent`.
404+
405+
```java
406+
package com.google.adk.examples;
407+
408+
import com.google.adk.agents.LlmAgent;
409+
import com.google.adk.tools.ToolPredicate;
410+
import com.google.adk.tools.mcp.McpToolset;
411+
import com.google.adk.tools.mcp.SseServerParameters;
412+
413+
import java.time.Duration;
414+
import java.util.Map;
415+
import java.util.Optional;
416+
417+
public class McpToolsExample {
418+
419+
public static void main(String[] args) {
420+
SseServerParameters sseServerParameters = SseServerParameters.builder()
421+
.url("https://your.sse-mcp.server/sse")
422+
.headers(Map.of(
423+
"Authorization", "Bearer your-mcp-api-key" // Optional: pass headers to you sse server
424+
))
425+
.timeout(Duration.ofSeconds(10L)) // timeout to connect to sse server
426+
.sseReadTimeout(Duration.ofSeconds(30L)) // timeout to tool call
427+
.build();
428+
var tools = new McpToolset(
429+
sseServerParameters,
430+
// Optional: Filter which tools from the MCP server are exposed
431+
Optional.of((ToolPredicate) (tool, readonlyContext) -> true)
432+
);
433+
LlmAgent llmAgent = LlmAgent.builder()
434+
.name("...")
435+
.description("...")
436+
.model("gemini-2.5-flash")
437+
.tools(tools)
438+
.build();
439+
}
440+
}
441+
442+
```
342443
343444
For Java, refer to the following sample to define an agent that initializes the `MCPToolset`:
344445

0 commit comments

Comments
 (0)