Skip to content

Commit 0d6c85e

Browse files
authored
feat: add Amazon Q agent configuration and MCP migration (#799)
1 parent 2f32588 commit 0d6c85e

File tree

4 files changed

+240
-0
lines changed

4 files changed

+240
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "default-agent",
3+
"version": "1.0.0",
4+
"description": "Default agent configuration",
5+
"mcpServers": {
6+
"smus-local-mcp": {
7+
"command": "python",
8+
"args": [
9+
"/etc/sagemaker-ui/sagemaker-mcp/smus-mcp.py"
10+
],
11+
"env": {}
12+
}
13+
},
14+
"tools": [
15+
"fsRead",
16+
"fsWrite",
17+
"fsReplace",
18+
"listDirectory",
19+
"fileSearch",
20+
"executeBash",
21+
"@smus-local-mcp"
22+
],
23+
"allowedTools": [
24+
"fsRead",
25+
"fsWrite",
26+
"fsReplace",
27+
"listDirectory",
28+
"fileSearch"
29+
],
30+
"toolsSettings": {
31+
"execute_bash": {
32+
"alwaysAllow": [
33+
{
34+
"preset": "readOnly"
35+
}
36+
]
37+
},
38+
"use_aws": {
39+
"alwaysAllow": [
40+
{
41+
"preset": "readOnly"
42+
}
43+
]
44+
}
45+
},
46+
"includedFiles": [],
47+
"resources": []
48+
}

template/v2/dirs/etc/sagemaker-ui/sagemaker_ui_post_startup.sh

100755100644
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,78 @@ else
303303
echo "Warning: MCP configuration file not found at $source_file"
304304
fi
305305

306+
# Migrating MCP configuration to new config file
307+
echo "Migrating MCP configuration to Amazon Q agents..."
308+
agents_target_file="$HOME/.aws/amazonq/agents/default.json"
309+
agents_source_file="/etc/sagemaker-ui/sagemaker-mcp/default.json"
310+
311+
if [ -f "$agents_source_file" ]; then
312+
mkdir -p "$HOME/.aws/amazonq/agents/"
313+
314+
if [ -f "$agents_target_file" ]; then
315+
echo "Existing Amazon Q agents configuration found, merging mcpServers..."
316+
317+
# Check if target file is valid JSON
318+
if jq empty "$agents_target_file" 2>/dev/null; then
319+
# Initialize mcpServers object if it doesn't exist in target
320+
if ! jq -e '.mcpServers' "$agents_target_file" >/dev/null 2>&1; then
321+
echo "Creating mcpServers object in existing agents configuration"
322+
jq '. + {"mcpServers":{}}' "$agents_target_file" > "$agents_target_file.tmp"
323+
mv "$agents_target_file.tmp" "$agents_target_file"
324+
fi
325+
326+
# Add servers from source that don't exist in target and update tools
327+
if jq -e '.mcpServers' "$agents_source_file" >/dev/null 2>&1; then
328+
source_server_names=$(jq -r '.mcpServers | keys[]' "$agents_source_file")
329+
330+
for server_name in $source_server_names; do
331+
if ! jq -e ".mcpServers.\"$server_name\"" "$agents_target_file" >/dev/null 2>&1; then
332+
# Server doesn't exist in target - add it
333+
server_config=$(jq ".mcpServers.\"$server_name\"" "$agents_source_file")
334+
jq --arg name "$server_name" --argjson config "$server_config" \
335+
'.mcpServers[$name] = $config' "$agents_target_file" > "$agents_target_file.tmp"
336+
mv "$agents_target_file.tmp" "$agents_target_file"
337+
echo "Added server '$server_name' to agents configuration"
338+
339+
# Check if source has tools that reference this server and add them
340+
server_tool_ref="@$server_name"
341+
if jq -e --arg tool "$server_tool_ref" '.tools | index($tool)' "$agents_source_file" >/dev/null 2>&1; then
342+
# Initialize tools array if it doesn't exist
343+
if ! jq -e '.tools' "$agents_target_file" >/dev/null 2>&1; then
344+
jq '. + {"tools":[]}' "$agents_target_file" > "$agents_target_file.tmp"
345+
mv "$agents_target_file.tmp" "$agents_target_file"
346+
fi
347+
348+
# Add tool reference if it doesn't exist
349+
if ! jq -e --arg tool "$server_tool_ref" '.tools | index($tool)' "$agents_target_file" >/dev/null 2>&1; then
350+
jq --arg tool "$server_tool_ref" '.tools += [$tool]' "$agents_target_file" > "$agents_target_file.tmp"
351+
mv "$agents_target_file.tmp" "$agents_target_file"
352+
echo "Added tool reference '$server_tool_ref' to agents configuration"
353+
fi
354+
fi
355+
else
356+
echo "Server '$server_name' already exists in configuration, skipping"
357+
fi
358+
done
359+
360+
echo "Successfully added missing mcpServers and tools from default.json to agents configuration"
361+
else
362+
echo "No mcpServers found in source configuration"
363+
fi
364+
else
365+
echo "Warning: Existing agents configuration is not valid JSON, replacing with default configuration"
366+
cp "$agents_source_file" "$agents_target_file"
367+
fi
368+
else
369+
cp "$agents_source_file" "$agents_target_file"
370+
echo "Created new Amazon Q agents configuration file"
371+
fi
372+
373+
echo "Successfully migrated MCP configuration to Amazon Q agents"
374+
else
375+
echo "Warning: Source configuration file not found at $agents_source_file"
376+
fi
377+
306378
# Generate sagemaker pysdk intelligent default config
307379
nohup python /etc/sagemaker/sm_pysdk_default_config.py &
308380
# Only run the following commands if SAGEMAKER_APP_TYPE_LOWERCASE is jupyterlab and domain is not in express mode
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "default-agent",
3+
"version": "1.0.0",
4+
"description": "Default agent configuration",
5+
"mcpServers": {
6+
"smus-local-mcp": {
7+
"command": "python",
8+
"args": [
9+
"/etc/sagemaker-ui/sagemaker-mcp/smus-mcp.py"
10+
],
11+
"env": {}
12+
}
13+
},
14+
"tools": [
15+
"fsRead",
16+
"fsWrite",
17+
"fsReplace",
18+
"listDirectory",
19+
"fileSearch",
20+
"executeBash",
21+
"@smus-local-mcp"
22+
],
23+
"allowedTools": [
24+
"fsRead",
25+
"fsWrite",
26+
"fsReplace",
27+
"listDirectory",
28+
"fileSearch"
29+
],
30+
"toolsSettings": {
31+
"execute_bash": {
32+
"alwaysAllow": [
33+
{
34+
"preset": "readOnly"
35+
}
36+
]
37+
},
38+
"use_aws": {
39+
"alwaysAllow": [
40+
{
41+
"preset": "readOnly"
42+
}
43+
]
44+
}
45+
},
46+
"includedFiles": [],
47+
"resources": []
48+
}

template/v3/dirs/etc/sagemaker-ui/sagemaker_ui_post_startup.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,78 @@ else
304304
echo "Warning: MCP configuration file not found at $source_file"
305305
fi
306306

307+
# Migrating MCP configuration to new config file
308+
echo "Migrating MCP configuration to Amazon Q agents..."
309+
agents_target_file="$HOME/.aws/amazonq/agents/default.json"
310+
agents_source_file="/etc/sagemaker-ui/sagemaker-mcp/default.json"
311+
312+
if [ -f "$agents_source_file" ]; then
313+
mkdir -p "$HOME/.aws/amazonq/agents/"
314+
315+
if [ -f "$agents_target_file" ]; then
316+
echo "Existing Amazon Q agents configuration found, merging mcpServers..."
317+
318+
# Check if target file is valid JSON
319+
if jq empty "$agents_target_file" 2>/dev/null; then
320+
# Initialize mcpServers object if it doesn't exist in target
321+
if ! jq -e '.mcpServers' "$agents_target_file" >/dev/null 2>&1; then
322+
echo "Creating mcpServers object in existing agents configuration"
323+
jq '. + {"mcpServers":{}}' "$agents_target_file" > "$agents_target_file.tmp"
324+
mv "$agents_target_file.tmp" "$agents_target_file"
325+
fi
326+
327+
# Add servers from source that don't exist in target and update tools
328+
if jq -e '.mcpServers' "$agents_source_file" >/dev/null 2>&1; then
329+
source_server_names=$(jq -r '.mcpServers | keys[]' "$agents_source_file")
330+
331+
for server_name in $source_server_names; do
332+
if ! jq -e ".mcpServers.\"$server_name\"" "$agents_target_file" >/dev/null 2>&1; then
333+
# Server doesn't exist in target - add it
334+
server_config=$(jq ".mcpServers.\"$server_name\"" "$agents_source_file")
335+
jq --arg name "$server_name" --argjson config "$server_config" \
336+
'.mcpServers[$name] = $config' "$agents_target_file" > "$agents_target_file.tmp"
337+
mv "$agents_target_file.tmp" "$agents_target_file"
338+
echo "Added server '$server_name' to agents configuration"
339+
340+
# Check if source has tools that reference this server and add them
341+
server_tool_ref="@$server_name"
342+
if jq -e --arg tool "$server_tool_ref" '.tools | index($tool)' "$agents_source_file" >/dev/null 2>&1; then
343+
# Initialize tools array if it doesn't exist
344+
if ! jq -e '.tools' "$agents_target_file" >/dev/null 2>&1; then
345+
jq '. + {"tools":[]}' "$agents_target_file" > "$agents_target_file.tmp"
346+
mv "$agents_target_file.tmp" "$agents_target_file"
347+
fi
348+
349+
# Add tool reference if it doesn't exist
350+
if ! jq -e --arg tool "$server_tool_ref" '.tools | index($tool)' "$agents_target_file" >/dev/null 2>&1; then
351+
jq --arg tool "$server_tool_ref" '.tools += [$tool]' "$agents_target_file" > "$agents_target_file.tmp"
352+
mv "$agents_target_file.tmp" "$agents_target_file"
353+
echo "Added tool reference '$server_tool_ref' to agents configuration"
354+
fi
355+
fi
356+
else
357+
echo "Server '$server_name' already exists in configuration, skipping"
358+
fi
359+
done
360+
361+
echo "Successfully added missing mcpServers and tools from default.json to agents configuration"
362+
else
363+
echo "No mcpServers found in source configuration"
364+
fi
365+
else
366+
echo "Warning: Existing agents configuration is not valid JSON, replacing with default configuration"
367+
cp "$agents_source_file" "$agents_target_file"
368+
fi
369+
else
370+
cp "$agents_source_file" "$agents_target_file"
371+
echo "Created new Amazon Q agents configuration file"
372+
fi
373+
374+
echo "Successfully migrated MCP configuration to Amazon Q agents"
375+
else
376+
echo "Warning: Source configuration file not found at $agents_source_file"
377+
fi
378+
307379
# Generate sagemaker pysdk intelligent default config
308380
nohup python /etc/sagemaker/sm_pysdk_default_config.py &
309381
# Only run the following commands if SAGEMAKER_APP_TYPE_LOWERCASE is jupyterlab and domain is not in express mode

0 commit comments

Comments
 (0)