Skip to content

Commit 08ecd08

Browse files
author
Yuriy
committed
feat(java-spring-ai-agents): centralize AgentCore dependency management and enhance auth
1 parent 05c1a4b commit 08ecd08

10 files changed

Lines changed: 85 additions & 93 deletions

File tree

apps/java-spring-ai-agents/aiagent/pom.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@
5454
<dependency>
5555
<groupId>org.springaicommunity</groupId>
5656
<artifactId>spring-ai-agentcore-code-interpreter</artifactId>
57-
<version>1.0.0-RC8</version>
5857
</dependency>
5958
<!-- AgentCore Browser -->
6059
<dependency>
6160
<groupId>org.springaicommunity</groupId>
6261
<artifactId>spring-ai-agentcore-browser</artifactId>
63-
<version>1.0.0-RC8</version>
6462
</dependency>
6563
<!-- Bedrock Runtime SDK for web grounding -->
6664
<dependency>
@@ -80,13 +78,11 @@
8078
<dependency>
8179
<groupId>org.springaicommunity</groupId>
8280
<artifactId>spring-ai-agentcore-memory</artifactId>
83-
<version>1.0.0-RC8</version>
8481
</dependency>
8582
<!-- AgentCore dependencies -->
8683
<dependency>
8784
<groupId>org.springaicommunity</groupId>
8885
<artifactId>spring-ai-agentcore-runtime-starter</artifactId>
89-
<version>1.0.0-RC8</version>
9086
</dependency>
9187
<dependency>
9288
<groupId>org.springframework.boot</groupId>
@@ -130,6 +126,13 @@
130126
<type>pom</type>
131127
<scope>import</scope>
132128
</dependency>
129+
<dependency>
130+
<groupId>org.springaicommunity</groupId>
131+
<artifactId>spring-ai-agentcore-bom</artifactId>
132+
<version>1.0.0-RC8</version>
133+
<type>pom</type>
134+
<scope>import</scope>
135+
</dependency>
133136
<dependency>
134137
<groupId>software.amazon.awssdk</groupId>
135138
<artifactId>bom</artifactId>

apps/java-spring-ai-agents/aiagent/src/main/java/com/example/agent/ChatService.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ public ChatService(AgentCoreMemory agentCoreMemory,
7373

7474
List<Advisor> advisors = new ArrayList<>();
7575

76-
if (advisors.size() > 0) {
77-
advisors.addAll(agentCoreMemory.advisors);
78-
logger.info("Advisors enabled: {} advisors", agentCoreMemory.advisors);
79-
}
76+
// Memory (STM + LTM)
77+
advisors.addAll(agentCoreMemory.advisors);
78+
logger.info("Memory enabled: {} advisors", agentCoreMemory.advisors.size());
8079

8180
// Knowledge Base (RAG)
8281
if (kbVectorStore != null) {
@@ -135,10 +134,10 @@ public Flux<String> chat(ChatRequest request, AgentCoreContext context) {
135134
String userPrompt = (request.prompt() != null && !request.prompt().trim().isEmpty())
136135
? request.prompt() : "Process this document";
137136
String combinedPrompt = userPrompt + "\n\nDocument analysis:\n" + documentAnalysis;
138-
return chat(combinedPrompt, getSessionId(context));
137+
return chat(combinedPrompt, getConversationId(context));
139138
});
140139
}
141-
return chat(request.prompt(), getSessionId(context));
140+
return chat(request.prompt(), getConversationId(context));
142141
}
143142

144143
private Flux<String> chat(String prompt, String sessionId) {
@@ -150,7 +149,7 @@ private Flux<String> chat(String prompt, String sessionId) {
150149
.contextWrite(ctx -> ctx.put(SessionConstants.SESSION_ID_KEY, sessionId));
151150
}
152151

153-
private String getSessionId(AgentCoreContext context) {
152+
private String getConversationId(AgentCoreContext context) {
154153
return ConversationIdResolver.resolve(context);
155154
}
156155

apps/java-spring-ai-agents/aiagent/src/main/resources/static/auth.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function clearAuth() {
4040

4141
function isAuthenticated() {
4242
const auth = loadAuth();
43-
if (!auth || !auth.expiresAt || !auth.accessToken) return false;
43+
if (!auth || !auth.expiresAt) return false;
44+
if (auth.authType !== 'simple' && !auth.accessToken) return false;
4445
return !isSessionExpired(auth);
4546
}
4647

@@ -49,7 +50,28 @@ function isSessionExpired(auth) {
4950
}
5051

5152
async function authenticateUser(username, password, config) {
52-
return authenticateCognito(username, password, config);
53+
if (config.authType === 'cognito') {
54+
return authenticateCognito(username, password, config);
55+
}
56+
return authenticateSimple(username, password);
57+
}
58+
59+
async function authenticateSimple(username, password) {
60+
if (!username || username.trim() === '') {
61+
throw new Error('Username is required');
62+
}
63+
64+
const auth = {
65+
username: username,
66+
expiresAt: Date.now() + (24 * 60 * 60 * 1000),
67+
authType: 'simple'
68+
};
69+
70+
// Use username as session ID for simple auth
71+
sessionStorage.setItem(SESSION_KEY, username);
72+
73+
saveAuth(auth);
74+
return auth;
5375
}
5476

5577
async function authenticateCognito(username, password, config) {

apps/java-spring-ai-agents/scripts/02-memory.sh

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ if [ -z "${AGENTCORE_MEMORY_MEMORY_ID}" ]; then
5151
done && echo " ACTIVE"
5252
fi
5353

54+
# Write memory config to application.properties
55+
grep -q "agentcore.memory.memory-id" ~/environment/aiagent/src/main/resources/application.properties 2>/dev/null || \
56+
cat >> ~/environment/aiagent/src/main/resources/application.properties << PROPS
57+
58+
# AgentCore Memory
59+
agentcore.memory.memory-id=${AGENTCORE_MEMORY_MEMORY_ID}
60+
agentcore.memory.long-term.auto-discovery=true
61+
PROPS
62+
5463
## Adding LTM strategies
5564

5665
# Check if strategies already exist
@@ -79,38 +88,14 @@ else
7988
done && echo " ACTIVE"
8089
fi
8190

82-
## Getting strategy IDs
83-
84-
echo ""
85-
echo "3. Get strategy IDs and save to environment file"
86-
87-
SEMANTIC_ID=$(aws bedrock-agentcore-control get-memory --memory-id "${AGENTCORE_MEMORY_MEMORY_ID}" \
88-
--no-cli-pager --query "memory.strategies[?name=='SemanticFacts'].strategyId | [0]" --output text)
89-
PREFS_ID=$(aws bedrock-agentcore-control get-memory --memory-id "${AGENTCORE_MEMORY_MEMORY_ID}" \
90-
--no-cli-pager --query "memory.strategies[?name=='UserPreferences'].strategyId | [0]" --output text)
91-
92-
# Update .envrc only if values changed
93-
if ! grep -q "AGENTCORE_MEMORY_LONG_TERM_SEMANTIC_STRATEGY_ID=${SEMANTIC_ID}" ~/environment/.envrc 2>/dev/null; then
94-
# Remove old entries if they exist
95-
sed -i.bak '/AGENTCORE_MEMORY_LONG_TERM_SEMANTIC_STRATEGY_ID/d' ~/environment/.envrc 2>/dev/null || true
96-
echo "export AGENTCORE_MEMORY_LONG_TERM_SEMANTIC_STRATEGY_ID=${SEMANTIC_ID}" >> ~/environment/.envrc
97-
fi
98-
99-
if ! grep -q "AGENTCORE_MEMORY_LONG_TERM_USER_PREFERENCE_STRATEGY_ID=${PREFS_ID}" ~/environment/.envrc 2>/dev/null; then
100-
# Remove old entries if they exist
101-
sed -i.bak '/AGENTCORE_MEMORY_LONG_TERM_USER_PREFERENCE_STRATEGY_ID/d' ~/environment/.envrc 2>/dev/null || true
102-
echo "export AGENTCORE_MEMORY_LONG_TERM_USER_PREFERENCE_STRATEGY_ID=${PREFS_ID}" >> ~/environment/.envrc
103-
fi
104-
105-
# Clean up backup files
106-
rm -f ~/environment/.envrc.bak
107-
10891
echo ""
10992
echo "=============================================="
11093
echo "Memory setup complete!"
11194
echo "=============================================="
11295
echo ""
11396
echo "Environment variables saved to ~/environment/.envrc:"
11497
echo " AGENTCORE_MEMORY_MEMORY_ID=${AGENTCORE_MEMORY_MEMORY_ID}"
115-
echo " AGENTCORE_MEMORY_LONG_TERM_SEMANTIC_STRATEGY_ID=${SEMANTIC_ID}"
116-
echo " AGENTCORE_MEMORY_LONG_TERM_USER_PREFERENCE_STRATEGY_ID=${PREFS_ID}"
98+
echo ""
99+
echo "Application properties written to application.properties:"
100+
echo " agentcore.memory.memory-id=${AGENTCORE_MEMORY_MEMORY_ID}"
101+
echo " agentcore.memory.long-term.auto-discovery=true"

apps/java-spring-ai-agents/scripts/03-knowledgebase.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,13 @@ else
144144
done && echo " ACTIVE"
145145
fi
146146

147-
# Save KB ID to environment
148-
if ! grep -q "SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID=${KB_ID}" ~/environment/.envrc 2>/dev/null; then
149-
sed -i.bak '/SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID/d' ~/environment/.envrc 2>/dev/null || true
150-
echo "export SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID=${KB_ID}" >> ~/environment/.envrc
151-
fi
147+
# Write KB config to application.properties
148+
grep -q "spring.ai.vectorstore.bedrock-knowledge-base.knowledge-base-id" ~/environment/aiagent/src/main/resources/application.properties 2>/dev/null || \
149+
cat >> ~/environment/aiagent/src/main/resources/application.properties << PROPS
150+
151+
# Knowledge Base
152+
spring.ai.vectorstore.bedrock-knowledge-base.knowledge-base-id=${KB_ID}
153+
PROPS
152154

153155
## Creating data source and ingesting documents
154156

@@ -205,5 +207,5 @@ echo "=============================================="
205207
echo "Knowledge Base setup complete!"
206208
echo "=============================================="
207209
echo ""
208-
echo "Environment variable saved to ~/environment/.envrc:"
209-
echo " SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID=${KB_ID}"
210+
echo "Application properties written to application.properties:"
211+
echo " spring.ai.vectorstore.bedrock-knowledge-base.knowledge-base-id=${KB_ID}"

apps/java-spring-ai-agents/scripts/06-mcp-gateway.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ if ! grep -q "GATEWAY_URL=" ~/environment/.envrc 2>/dev/null; then
148148
echo "export GATEWAY_URL=${GATEWAY_URL}" >> ~/environment/.envrc
149149
fi
150150

151+
# Write MCP client config to application.properties
152+
grep -q "spring.ai.mcp.client.streamablehttp.connections.gateway.url" ~/environment/aiagent/src/main/resources/application.properties 2>/dev/null || \
153+
cat >> ~/environment/aiagent/src/main/resources/application.properties << PROPS
154+
155+
# MCP Client
156+
spring.ai.mcp.client.toolcallback.enabled=true
157+
spring.ai.mcp.client.initialized=false
158+
spring.ai.mcp.client.streamablehttp.connections.gateway.url=${GATEWAY_URL}
159+
PROPS
160+
151161
## Adding the backoffice target
152162

153163
echo ""

apps/java-spring-ai-agents/scripts/07-aiagent-cognito.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,17 @@ done
141141

142142
echo "Test users: admin, alice, bob"
143143

144-
## Save issuer URI for Spring Security
144+
## Write security config to application.properties
145145

146146
echo ""
147-
echo "4. Save issuer URI for Spring Security"
147+
echo "4. Write security config to application.properties"
148148

149-
SPRING_ISSUER_URI="https://cognito-idp.${AWS_REGION}.amazonaws.com/${AIAGENT_USER_POOL_ID}"
150-
if ! grep -q "SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI=" ~/environment/.envrc 2>/dev/null; then
151-
sed -i.bak '/SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI=/d' ~/environment/.envrc 2>/dev/null || true
152-
echo "export SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI=${SPRING_ISSUER_URI}" >> ~/environment/.envrc
153-
fi
149+
grep -q "spring.security.oauth2.resourceserver.jwt.issuer-uri" ~/environment/aiagent/src/main/resources/application.properties 2>/dev/null || \
150+
cat >> ~/environment/aiagent/src/main/resources/application.properties << PROPS
151+
152+
# Security
153+
spring.security.oauth2.resourceserver.jwt.issuer-uri=https://cognito-idp.${AWS_REGION}.amazonaws.com/${AIAGENT_USER_POOL_ID}
154+
PROPS
154155

155156
# Clean up backup files
156157
rm -f ~/environment/.envrc.bak
@@ -165,4 +166,7 @@ echo " AIAGENT_USER_POOL_ID=${AIAGENT_USER_POOL_ID}"
165166
echo " AIAGENT_CLIENT_ID=${AIAGENT_CLIENT_ID}"
166167
echo " AIAGENT_DISCOVERY_URL=${AIAGENT_DISCOVERY_URL}"
167168
echo ""
169+
echo "Application properties written to application.properties:"
170+
echo " spring.security.oauth2.resourceserver.jwt.issuer-uri=https://cognito-idp.${AWS_REGION}.amazonaws.com/${AIAGENT_USER_POOL_ID}"
171+
echo ""
168172
echo "Test users created: admin, alice, bob (password: \${IDE_PASSWORD})"

apps/java-spring-ai-agents/scripts/08-aiagent-runtime.sh

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ if [ -z "${AIAGENT_USER_POOL_ID}" ] || [ -z "${AIAGENT_CLIENT_ID}" ] || [ -z "${
2020
exit 1
2121
fi
2222

23-
if [ -z "${AGENTCORE_MEMORY_MEMORY_ID}" ]; then
24-
echo "Error: Missing Memory variables. Run 02-memory.sh first."
25-
exit 1
26-
fi
27-
2823
# Get account ID and region
2924
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text --no-cli-pager)
3025
AWS_REGION=$(aws configure get region)
@@ -179,17 +174,6 @@ EXISTING_RUNTIME_ID=$(aws bedrock-agentcore-control list-agent-runtimes \
179174
--region ${AWS_REGION} --no-cli-pager \
180175
--query "agentRuntimes[?agentRuntimeName=='aiagent'].agentRuntimeId | [0]" --output text 2>/dev/null || echo "None")
181176

182-
# Build environment variables JSON
183-
cat > /tmp/aiagent-env.json << EOF
184-
{
185-
"AGENTCORE_MEMORY_MEMORY_ID": "${AGENTCORE_MEMORY_MEMORY_ID}",
186-
"AGENTCORE_MEMORY_LONG_TERM_SEMANTIC_STRATEGY_ID": "${AGENTCORE_MEMORY_LONG_TERM_SEMANTIC_STRATEGY_ID}",
187-
"AGENTCORE_MEMORY_LONG_TERM_USER_PREFERENCE_STRATEGY_ID": "${AGENTCORE_MEMORY_LONG_TERM_USER_PREFERENCE_STRATEGY_ID}",
188-
"SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID": "${SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID}",
189-
"SPRING_AI_MCP_CLIENT_STREAMABLEHTTP_CONNECTIONS_GATEWAY_URL": "${GATEWAY_URL}"
190-
}
191-
EOF
192-
193177
if [ "${EXISTING_RUNTIME_ID}" != "None" ] && [ -n "${EXISTING_RUNTIME_ID}" ]; then
194178
echo "AgentCore Runtime already exists: ${EXISTING_RUNTIME_ID}"
195179
echo "Updating runtime with latest configuration..."
@@ -201,8 +185,7 @@ if [ "${EXISTING_RUNTIME_ID}" != "None" ] && [ -n "${EXISTING_RUNTIME_ID}" ]; th
201185
--agent-runtime-artifact "{\"containerConfiguration\":{\"containerUri\":\"${ECR_URI}:latest\"}}" \
202186
--network-configuration "{\"networkMode\":\"VPC\",\"networkModeConfig\":{\"subnets\":[\"${SUBNET_ID}\"],\"securityGroups\":[\"${SG_ID}\"]}}" \
203187
--authorizer-configuration "{\"customJWTAuthorizer\":{\"discoveryUrl\":\"${AIAGENT_DISCOVERY_URL}\",\"allowedClients\":[\"${AIAGENT_CLIENT_ID}\"]}}" \
204-
--request-header-configuration '{"requestHeaderAllowlist":["Authorization"]}' \
205-
--environment-variables file:///tmp/aiagent-env.json \
188+
--request-header-configuration '{"requestHeaderAllowlist":["Authorization","X-Amzn-Bedrock-AgentCore-Runtime-Session-Id"]}' \
206189
--region ${AWS_REGION} \
207190
--no-cli-pager
208191

@@ -221,8 +204,7 @@ else
221204
--agent-runtime-artifact "{\"containerConfiguration\":{\"containerUri\":\"${ECR_URI}:latest\"}}" \
222205
--network-configuration "{\"networkMode\":\"VPC\",\"networkModeConfig\":{\"subnets\":[\"${SUBNET_ID}\"],\"securityGroups\":[\"${SG_ID}\"]}}" \
223206
--authorizer-configuration "{\"customJWTAuthorizer\":{\"discoveryUrl\":\"${AIAGENT_DISCOVERY_URL}\",\"allowedClients\":[\"${AIAGENT_CLIENT_ID}\"]}}" \
224-
--request-header-configuration '{"requestHeaderAllowlist":["Authorization"]}' \
225-
--environment-variables file:///tmp/aiagent-env.json \
207+
--request-header-configuration '{"requestHeaderAllowlist":["Authorization","X-Amzn-Bedrock-AgentCore-Runtime-Session-Id"]}' \
226208
--region ${AWS_REGION} \
227209
--no-cli-pager \
228210
--query 'agentRuntimeId' --output text)
@@ -235,8 +217,6 @@ else
235217
done && echo " READY"
236218
fi
237219

238-
rm -f /tmp/aiagent-env.json
239-
240220
# Save runtime ID to environment
241221
if ! grep -q "AIAGENT_RUNTIME_ID=${AIAGENT_RUNTIME_ID}" ~/environment/.envrc 2>/dev/null; then
242222
sed -i.bak '/AIAGENT_RUNTIME_ID=/d' ~/environment/.envrc 2>/dev/null || true

apps/java-spring-ai-agents/scripts/12-aiagent-redeploy.sh

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,16 @@ docker push "${ECR_URI}:latest"
5656
echo ""
5757
echo "2. Update the AgentCore Runtime"
5858

59-
# Build environment variables JSON
60-
cat > /tmp/aiagent-env.json << EOF
61-
{
62-
"AGENTCORE_MEMORY_MEMORY_ID": "${AGENTCORE_MEMORY_MEMORY_ID}",
63-
"AGENTCORE_MEMORY_LONG_TERM_SEMANTIC_STRATEGY_ID": "${AGENTCORE_MEMORY_LONG_TERM_SEMANTIC_STRATEGY_ID}",
64-
"AGENTCORE_MEMORY_LONG_TERM_USER_PREFERENCE_STRATEGY_ID": "${AGENTCORE_MEMORY_LONG_TERM_USER_PREFERENCE_STRATEGY_ID}",
65-
"SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID": "${SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID}",
66-
"SPRING_AI_MCP_CLIENT_STREAMABLEHTTP_CONNECTIONS_GATEWAY_URL": "${GATEWAY_URL}"
67-
}
68-
EOF
69-
7059
aws bedrock-agentcore-control update-agent-runtime \
7160
--agent-runtime-id "${AIAGENT_RUNTIME_ID}" \
7261
--role-arn "arn:aws:iam::${ACCOUNT_ID}:role/aiagent-runtime-role" \
7362
--agent-runtime-artifact "{\"containerConfiguration\":{\"containerUri\":\"${ECR_URI}:latest\"}}" \
7463
--network-configuration "{\"networkMode\":\"VPC\",\"networkModeConfig\":{\"subnets\":[\"${SUBNET_ID}\"],\"securityGroups\":[\"${SG_ID}\"]}}" \
7564
--authorizer-configuration "{\"customJWTAuthorizer\":{\"discoveryUrl\":\"${AIAGENT_DISCOVERY_URL}\",\"allowedClients\":[\"${AIAGENT_CLIENT_ID}\"]}}" \
76-
--request-header-configuration '{"requestHeaderAllowlist":["Authorization"]}' \
77-
--environment-variables file:///tmp/aiagent-env.json \
65+
--request-header-configuration '{"requestHeaderAllowlist":["Authorization","X-Amzn-Bedrock-AgentCore-Runtime-Session-Id"]}' \
7866
--region ${AWS_REGION} \
7967
--no-cli-pager
8068

81-
rm -f /tmp/aiagent-env.json
82-
8369
echo -n "Waiting for runtime"
8470
while [ "$(aws bedrock-agentcore-control get-agent-runtime \
8571
--agent-runtime-id "${AIAGENT_RUNTIME_ID}" --region ${AWS_REGION} \

apps/java-spring-ai-agents/scripts/99-cleanup.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ fi
317317
echo ""
318318
echo "## Deleting Knowledge Base"
319319

320-
KB_ID="${SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID}"
320+
KB_ID=$(aws bedrock-agent list-knowledge-bases --no-cli-pager \
321+
--query "knowledgeBaseSummaries[?name=='aiagent-kb'].knowledgeBaseId | [0]" --output text 2>/dev/null || echo "")
321322
if [ -n "${KB_ID}" ]; then
322323
# Delete data sources first
323324
DS_IDS=$(aws bedrock-agent list-data-sources --knowledge-base-id "${KB_ID}" --no-cli-pager \

0 commit comments

Comments
 (0)