Skip to content

Commit 5945549

Browse files
authored
fix: configure mcp gateway tool timeout (#4783)
1 parent 5823b8e commit 5945549

7 files changed

Lines changed: 415 additions & 3 deletions

File tree

AGENTS.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# AGENTS.md - AI Assistant Guide for Spring AI Alibaba
2+
3+
This file provides guidance for AI assistants working with the Spring AI Alibaba codebase.
4+
5+
## Project Overview
6+
7+
Spring AI Alibaba is a production-ready framework for building Agentic, Workflow, and Multi-agent applications. It is an implementation of the Spring AI framework tailored for Alibaba Cloud services and components. It provides a comprehensive ecosystem for developing AI-powered applications with built-in context engineering and human-in-the-loop support.
8+
9+
**Key Features:**
10+
11+
- Multi-Agent Orchestration with built-in patterns
12+
- Context Engineering with human-in-the-loop, context compaction, editing, model call limits
13+
- Graph-based workflow with conditional routing, nested graphs, parallel execution
14+
- A2A (Agent-to-Agent) support with Nacos integration
15+
- Rich model support (DashScope, OpenAI, DeepSeek) and MCP (Model Context Protocol)
16+
- One-stop visual agent platform
17+
18+
## Repository Structure
19+
20+
```
21+
spring-ai-alibaba/
22+
├── spring-ai-alibaba-agent-framework/ # Multi-agent framework (Sequential, Parallel, Routing, etc.)
23+
├── spring-ai-alibaba-graph-core/ # Runtime providing persistence, workflow orchestration, state mgmt
24+
├── spring-ai-alibaba-studio/ # Embedded UI for debugging agents visually
25+
├── spring-ai-alibaba-admin/ # One-stop Agent platform (visual dev, observability, MCP mgmt)
26+
├── spring-ai-alibaba-bom/ # Bill of Materials for dependency management
27+
├── spring-boot-starters/ # Spring Boot Starters
28+
│ ├── spring-ai-alibaba-starter-a2a-nacos/ # Nacos A2A communication
29+
│ ├── spring-ai-alibaba-starter-builtin-nodes/ # Built-in workflow nodes
30+
│ ├── spring-ai-alibaba-starter-config-nacos/ # Dynamic config with Nacos
31+
│ └── spring-ai-alibaba-starter-graph-observation/ # Observability
32+
├── examples/ # Example applications
33+
│ ├── chatbot/ # Chatbot example
34+
│ ├── deepresearch/ # Deep research agent example
35+
│ └── documentation/ # Documentation examples
36+
├── tools/ # Build and linting tools
37+
└── docs/ # Documentation
38+
```
39+
40+
## Build System
41+
42+
### Prerequisites
43+
44+
- **JDK**: 17 (Required by `java.version` property)
45+
- **Maven**: 3.6+
46+
- **Git**
47+
48+
### Common Build Commands
49+
50+
```shell
51+
# Build the entire project (skip tests)
52+
./mvnw -B package -DskipTests=true
53+
54+
# Build a specific module
55+
./mvnw -pl :spring-ai-alibaba-agent-framework -B package -DskipTests=true
56+
57+
# Clean project
58+
./mvnw clean
59+
60+
# Run tests
61+
./mvnw test
62+
63+
# Run linting checks (using Makefile)
64+
make lint
65+
make licenses-check
66+
```
67+
68+
## Architecture & Key Concepts
69+
70+
### Core Components
71+
72+
- **Agent Framework**: Built-in agents like `SequentialAgent`, `ParallelAgent`, `RoutingAgent`, `LoopAgent`.
73+
- **Graph Core**: Underlying engine for stateful agents, supporting persistence (PostgreSQL, MySQL, Oracle, MongoDB, Redis, File).
74+
- **A2A (Agent-to-Agent)**: Enables agents to seek and communicate with each other using Nacos as a registry.
75+
- **Admin & Studio**: Provides visual tools for developing and debugging agent workflows.
76+
77+
### Technology Stack
78+
79+
- **Framework**: Spring Boot 3.5.x, Spring AI 1.1.x
80+
- **Cloud Integration**: Alibaba Cloud DashScope, Nacos (Service Discovery & Config)
81+
- **Observability**: Spring Cloud Observation (Micrometer/OpenTelemetry)
82+
83+
## Code Style & Conventions
84+
85+
### General Guidelines
86+
87+
- Follow **Spring AI** standard code formatting.
88+
- Use **Apache 2.0** license headers for all Java files.
89+
- **Java 17** features are encouraged (records, switch expressions, text blocks).
90+
- Avoid `System.out.println` - use SLF4J logging.
91+
- Use `final` for local variables and parameters where appropriate.
92+
- Use Lombok annotations (`@Data`, `@Slf4j`, etc.) to reduce boilerplate.
93+
94+
### Linting & Formatting
95+
96+
The project uses `make` for linting tasks:
97+
- `make codespell`: Checks for spelling errors.
98+
- `make yaml-lint`: Checks YAML file formatting.
99+
- `make licenses-check`: Verifies license headers.
100+
101+
### License Header
102+
103+
```java
104+
/*
105+
* Copyright 2025-2026 the original author or authors.
106+
*
107+
* Licensed under the Apache License, Version 2.0 (the "License");
108+
* you may not use this file except in compliance with the License.
109+
* You may obtain a copy of the License at
110+
*
111+
* https://www.apache.org/licenses/LICENSE-2.0
112+
*
113+
* Unless required by applicable law or agreed to in writing, software
114+
* distributed under the License is distributed on an "AS IS" BASIS,
115+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
116+
* See the License for the specific language governing permissions and
117+
* limitations under the License.
118+
*/
119+
```
120+
121+
## Testing
122+
123+
### Frameworks
124+
125+
- **JUnit 5** (`org.junit.jupiter`)
126+
- **Mockito**
127+
128+
### Running Tests
129+
130+
```shell
131+
# Run all tests
132+
./mvnw test
133+
134+
# Run a specific test class
135+
./mvnw -pl :<module-name> -Dtest=<TestClassName> test
136+
```
137+
138+
## Tips for AI Assistants
139+
140+
1. **JDK Version**: Project targets JDK 17. Use appropriate language features.
141+
2. **Spring Boot**: Uses Spring Boot 3.x. Be aware of `jakarta.*` namespace vs `javax.*`.
142+
3. **Dependencies**: Check `spring-ai-alibaba-bom` or parent pom for version management.
143+
4. **Makefile**: Use the Makefile in the root for project maintenance tasks (linting, license checks).
144+
5. **Structure**: When adding new features, prefer creating or updating modules within `spring-ai-alibaba-agent-framework` or `spring-boot-starters` depending on the scope.
145+
146+
## Important Links
147+
148+
- **Issues**: [https://github.com/alibaba/spring-ai-alibaba/issues](https://github.com/alibaba/spring-ai-alibaba/issues)
149+
- **Source**: [https://github.com/alibaba/spring-ai-alibaba](https://github.com/alibaba/spring-ai-alibaba)
150+
- **Contributing**: [CONTRIBUTING.md](CONTRIBUTING.md)

spring-boot-starters/spring-ai-alibaba-starter-config-nacos/src/main/java/com/alibaba/cloud/ai/agent/nacos/NacosMcpToolsInjector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public static McpServersVO getMcpServersVO(NacosOptions nacosOptions) {
4747
public static List<ToolCallback> convert(NacosOptions nacosOptions, McpServersVO mcpServersVO) {
4848

4949
NacosMcpGatewayToolsInitializer nacosMcpGatewayToolsInitializer = new NacosMcpGatewayToolsInitializer(
50-
nacosOptions.mcpOperationService, mcpServersVO.getMcpServers());
50+
nacosOptions.mcpOperationService, mcpServersVO.getMcpServers(),
51+
nacosOptions.getMcpGatewayToolTimeout());
5152
return Collections.unmodifiableList(nacosMcpGatewayToolsInitializer.initializeTools());
5253
}
5354

spring-boot-starters/spring-ai-alibaba-starter-config-nacos/src/main/java/com/alibaba/cloud/ai/agent/nacos/NacosOptions.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.alibaba.cloud.ai.agent.nacos;
1818

19+
import java.time.Duration;
1920
import java.util.Properties;
2021

2122
import com.alibaba.cloud.ai.mcp.nacos.service.NacosMcpOperationService;
@@ -25,6 +26,8 @@
2526
import com.alibaba.nacos.maintainer.client.ai.AiMaintainerService;
2627
import com.alibaba.nacos.maintainer.client.ai.NacosAiMaintainerServiceImpl;
2728

29+
import org.springframework.boot.convert.DurationStyle;
30+
2831
public class NacosOptions {
2932

3033
protected boolean encrypted;
@@ -51,6 +54,8 @@ public class NacosOptions {
5154

5255
private String mcpNamespace;
5356

57+
private Duration mcpGatewayToolTimeout;
58+
5459
private void encryptParamInit(Properties properties) {
5560
encrypted = Boolean.parseBoolean(properties.getProperty("encrypted", "false"));
5661
String defaultEncrypted = String.valueOf(encrypted);
@@ -70,6 +75,7 @@ public NacosOptions(Properties properties) throws NacosException {
7075
encryptParamInit(properties);
7176
agentName = properties.getProperty("agentName");
7277
mcpNamespace = properties.getProperty("mcpNamespace", properties.getProperty("namespace"));
78+
mcpGatewayToolTimeout = parseMcpGatewayToolTimeout(properties);
7379
String rawLabels = properties.getProperty("nacos.app.conn.labels", "");
7480
if (StringUtils.isBlank(rawLabels)) {
7581
rawLabels = "AgentName=" + agentName;
@@ -85,6 +91,19 @@ public NacosOptions(Properties properties) throws NacosException {
8591

8692
}
8793

94+
private Duration parseMcpGatewayToolTimeout(Properties properties) {
95+
String timeout = properties.getProperty("mcpGatewayToolTimeout");
96+
if (!StringUtils.isBlank(properties.getProperty("mcp-gateway-tool-timeout"))) {
97+
timeout = properties.getProperty("mcp-gateway-tool-timeout");
98+
}
99+
if (StringUtils.isBlank(timeout)) {
100+
return null;
101+
}
102+
properties.remove("mcpGatewayToolTimeout");
103+
properties.remove("mcp-gateway-tool-timeout");
104+
return DurationStyle.detectAndParse(timeout);
105+
}
106+
88107
public boolean isEncrypted() {
89108
return encrypted;
90109
}
@@ -181,4 +200,12 @@ public void setMcpNamespace(String mcpNamespace) {
181200
this.mcpNamespace = mcpNamespace;
182201
}
183202

203+
public Duration getMcpGatewayToolTimeout() {
204+
return mcpGatewayToolTimeout;
205+
}
206+
207+
public void setMcpGatewayToolTimeout(Duration mcpGatewayToolTimeout) {
208+
this.mcpGatewayToolTimeout = mcpGatewayToolTimeout;
209+
}
210+
184211
}

spring-boot-starters/spring-ai-alibaba-starter-config-nacos/src/main/java/com/alibaba/cloud/ai/agent/nacos/tools/NacosMcpGatewayToolCallback.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.alibaba.cloud.ai.agent.nacos.tools;
1818

19+
import java.time.Duration;
1920
import java.util.HashMap;
2021
import java.util.Iterator;
2122
import java.util.List;
@@ -84,14 +85,27 @@ public class NacosMcpGatewayToolCallback implements ToolCallback {
8485

8586
McpServersVO.McpServerVO mcpServerVO;
8687

88+
private final Duration requestTimeout;
89+
8790
/**
8891
* Instantiates a new Nacos mcp gateway tool callback.
8992
* @param toolDefinition the tool definition
9093
*/
9194
public NacosMcpGatewayToolCallback(final McpGatewayToolDefinition toolDefinition, NacosMcpOperationService nacosMcpOperationService, McpServersVO.McpServerVO mcpServersVO) {
95+
this(toolDefinition, nacosMcpOperationService, mcpServersVO, null);
96+
}
97+
98+
public NacosMcpGatewayToolCallback(final McpGatewayToolDefinition toolDefinition,
99+
NacosMcpOperationService nacosMcpOperationService, McpServersVO.McpServerVO mcpServersVO,
100+
Duration requestTimeout) {
92101
this.toolDefinition = (NacosMcpGatewayToolDefinition) toolDefinition;
93102
this.nacosMcpOperationService = nacosMcpOperationService;
94103
this.mcpServerVO = mcpServersVO;
104+
this.requestTimeout = requestTimeout;
105+
}
106+
107+
public Duration getRequestTimeout() {
108+
return requestTimeout;
95109
}
96110

97111
/**
@@ -528,7 +542,7 @@ private String handleMcpStreamProtocol(Map<String, Object> args, McpServerRemote
528542
HttpClientSseClientTransport transport = transportBuilder.build();
529543

530544
// 创建MCP同步客户端
531-
McpSyncClient client = McpClient.sync(transport).build();
545+
McpSyncClient client = configureTimeouts(McpClient.sync(transport)).build();
532546
try {
533547
// 初始化客户端
534548
InitializeResult initializeResult = client.initialize();
@@ -583,6 +597,14 @@ else if (first instanceof Map<?, ?> map && map.containsKey("text")) {
583597
}
584598
}
585599

600+
McpClient.SyncSpec configureTimeouts(McpClient.SyncSpec clientSpec) {
601+
if (requestTimeout != null) {
602+
clientSpec.requestTimeout(requestTimeout);
603+
clientSpec.initializationTimeout(requestTimeout);
604+
}
605+
return clientSpec;
606+
}
607+
586608
/**
587609
* Close.
588610
*/

spring-boot-starters/spring-ai-alibaba-starter-config-nacos/src/main/java/com/alibaba/cloud/ai/agent/nacos/tools/NacosMcpGatewayToolsInitializer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.alibaba.cloud.ai.agent.nacos.tools;
1818

19+
import java.time.Duration;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122
import java.util.Map;
@@ -41,9 +42,17 @@ public class NacosMcpGatewayToolsInitializer {
4142

4243
private List<McpServersVO.McpServerVO> mcpServers;
4344

45+
private final Duration requestTimeout;
46+
4447
public NacosMcpGatewayToolsInitializer(NacosMcpOperationService nacosMcpOperationService, List<McpServersVO.McpServerVO> mcpServers) {
48+
this(nacosMcpOperationService, mcpServers, null);
49+
}
50+
51+
public NacosMcpGatewayToolsInitializer(NacosMcpOperationService nacosMcpOperationService, List<McpServersVO.McpServerVO> mcpServers,
52+
Duration requestTimeout) {
4553
this.nacosMcpOperationService = nacosMcpOperationService;
4654
this.mcpServers = mcpServers;
55+
this.requestTimeout = requestTimeout;
4756
}
4857

4958
public List<ToolCallback> initializeTools() {
@@ -122,7 +131,8 @@ private List<ToolCallback> parseToolsFromMcpServerDetailInfo(McpServerDetailInfo
122131
.remoteServerConfig(mcpServerRemoteServiceConfig)
123132
.toolsMeta(metaInfo)
124133
.build();
125-
toolCallbacks.add(new NacosMcpGatewayToolCallback(toolDefinition, nacosMcpOperationService, serverVO));
134+
toolCallbacks.add(new NacosMcpGatewayToolCallback(toolDefinition, nacosMcpOperationService, serverVO,
135+
requestTimeout));
126136
}
127137
}
128138
return toolCallbacks;

0 commit comments

Comments
 (0)