Skip to content

Commit 06d7bd3

Browse files
authored
Merge pull request #23 from mojwang/fix/mcp-api-key-messaging
fix: improve MCP API key status messaging
2 parents a10397e + fed6fe2 commit 06d7bd3

3 files changed

Lines changed: 214 additions & 0 deletions

File tree

scripts/fix-mcp-servers.sh

100755100644
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ for server in "${servers[@]}"; do
108108
if [[ -z "${!api_key_var}" ]]; then
109109
print_warning "Skipping $server ($api_key_var not set)"
110110
continue
111+
else
112+
print_info "$server API key is configured ($api_key_var)"
111113
fi
112114
fi
113115

scripts/setup-claude-code-mcp.sh

100755100644
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ add_claude_code_server() {
4141
if [[ -z "${!api_key_var}" ]]; then
4242
print_warning "Skipping $server_name ($api_key_var not set)"
4343
return 1
44+
else
45+
print_info "$server_name API key is configured ($api_key_var)"
4446
fi
4547
fi
4648

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
#!/usr/bin/env bash
2+
3+
# Test MCP API key messaging improvements
4+
5+
set -e
6+
7+
# Set test mode
8+
export TEST_MODE=1
9+
10+
# Set ROOT_DIR correctly before loading test framework
11+
export ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
12+
13+
# Load test framework
14+
source "$(dirname "$0")/../test_framework.sh"
15+
16+
# Mock functions and variables needed for testing
17+
source "$ROOT_DIR/lib/common.sh"
18+
19+
# Mock the print functions to capture output
20+
mock_output=""
21+
print_info() {
22+
mock_output="$mock_output[INFO] $*\n"
23+
}
24+
print_warning() {
25+
mock_output="$mock_output[WARNING] $*\n"
26+
}
27+
print_success() {
28+
mock_output="$mock_output[SUCCESS] $*\n"
29+
}
30+
print_error() {
31+
mock_output="$mock_output[ERROR] $*\n"
32+
}
33+
34+
# Test suite
35+
describe "MCP API Key Messaging"
36+
37+
# Test positive message when API key is configured
38+
it "shows positive message when API key is configured in setup-claude-code-mcp" '
39+
# Setup
40+
export EXA_API_KEY="test-key-123"
41+
mock_output=""
42+
43+
# Mock the required associative arrays
44+
declare -A MCP_SERVER_API_KEYS
45+
MCP_SERVER_API_KEYS["exa"]="EXA_API_KEY"
46+
47+
# Mock function to test just the API key checking logic
48+
check_api_key_message() {
49+
local server_name="$1"
50+
local api_key_var="${MCP_SERVER_API_KEYS[$server_name]:-}"
51+
52+
if [[ -n "$api_key_var" ]] && [[ "$server_name" != "taskmaster" ]]; then
53+
if [[ -z "${!api_key_var}" ]]; then
54+
print_warning "Skipping $server_name ($api_key_var not set)"
55+
return 1
56+
else
57+
print_info "$server_name API key is configured ($api_key_var)"
58+
fi
59+
fi
60+
return 0
61+
}
62+
63+
# Execute
64+
check_api_key_message "exa"
65+
66+
# Verify
67+
assert_contains "$mock_output" "exa API key is configured (EXA_API_KEY)" "Should show positive API key message"
68+
assert_not_contains "$mock_output" "not set" "Should not show not set message"
69+
'
70+
71+
# Test warning message when API key is not configured
72+
it "shows warning message when API key is not configured" '
73+
# Setup
74+
unset EXA_API_KEY
75+
mock_output=""
76+
77+
# Mock the required associative arrays
78+
declare -A MCP_SERVER_API_KEYS
79+
MCP_SERVER_API_KEYS["exa"]="EXA_API_KEY"
80+
81+
# Mock function to test just the API key checking logic
82+
check_api_key_message() {
83+
local server_name="$1"
84+
local api_key_var="${MCP_SERVER_API_KEYS[$server_name]:-}"
85+
86+
if [[ -n "$api_key_var" ]] && [[ "$server_name" != "taskmaster" ]]; then
87+
if [[ -z "${!api_key_var}" ]]; then
88+
print_warning "Skipping $server_name ($api_key_var not set)"
89+
return 1
90+
else
91+
print_info "$server_name API key is configured ($api_key_var)"
92+
fi
93+
fi
94+
return 0
95+
}
96+
97+
# Execute
98+
check_api_key_message "exa" || true
99+
100+
# Verify
101+
assert_contains "$mock_output" "Skipping exa (EXA_API_KEY not set)" "Should show not set warning"
102+
assert_not_contains "$mock_output" "configured" "Should not show configured message"
103+
'
104+
105+
# Test that TaskMaster is treated specially (optional API keys)
106+
it "handles TaskMaster as special case with optional API keys" '
107+
# Setup
108+
unset ANTHROPIC_API_KEY
109+
mock_output=""
110+
111+
# Mock the required associative arrays
112+
declare -A MCP_SERVER_API_KEYS
113+
MCP_SERVER_API_KEYS["taskmaster"]="ANTHROPIC_API_KEY"
114+
115+
# Mock function to test just the API key checking logic
116+
check_api_key_message() {
117+
local server_name="$1"
118+
local api_key_var="${MCP_SERVER_API_KEYS[$server_name]:-}"
119+
120+
if [[ -n "$api_key_var" ]] && [[ "$server_name" != "taskmaster" ]]; then
121+
if [[ -z "${!api_key_var}" ]]; then
122+
print_warning "Skipping $server_name ($api_key_var not set)"
123+
return 1
124+
else
125+
print_info "$server_name API key is configured ($api_key_var)"
126+
fi
127+
fi
128+
return 0
129+
}
130+
131+
# Execute
132+
check_api_key_message "taskmaster"
133+
local result=$?
134+
135+
# Verify - TaskMaster should not show any API key messages (it is optional)
136+
assert_equals "$result" "0" "TaskMaster should return success even without API key"
137+
assert_equals "$mock_output" "" "TaskMaster should not show API key messages"
138+
'
139+
140+
# Test with Figma API key
141+
it "shows positive message for Figma when API key is configured" '
142+
# Setup
143+
export FIGMA_API_KEY="test-figma-key"
144+
mock_output=""
145+
146+
# Mock the required associative arrays
147+
declare -A MCP_SERVER_API_KEYS
148+
MCP_SERVER_API_KEYS["figma"]="FIGMA_API_KEY"
149+
150+
# Mock function to test just the API key checking logic
151+
check_api_key_message() {
152+
local server_name="$1"
153+
local api_key_var="${MCP_SERVER_API_KEYS[$server_name]:-}"
154+
155+
if [[ -n "$api_key_var" ]] && [[ "$server_name" != "taskmaster" ]]; then
156+
if [[ -z "${!api_key_var}" ]]; then
157+
print_warning "Skipping $server_name ($api_key_var not set)"
158+
return 1
159+
else
160+
print_info "$server_name API key is configured ($api_key_var)"
161+
fi
162+
fi
163+
return 0
164+
}
165+
166+
# Execute
167+
check_api_key_message "figma"
168+
169+
# Verify
170+
assert_contains "$mock_output" "figma API key is configured (FIGMA_API_KEY)" "Should show positive Figma API key message"
171+
'
172+
173+
# Test behavior in fix-mcp-servers.sh context
174+
it "shows positive message in fix-mcp-servers context" '
175+
# Setup
176+
export EXA_API_KEY="test-key"
177+
mock_output=""
178+
179+
# Mock the required associative arrays
180+
declare -A MCP_SERVER_API_KEYS
181+
MCP_SERVER_API_KEYS["exa"]="EXA_API_KEY"
182+
183+
# Mock the fix-mcp-servers logic
184+
fix_mcp_check_api_key() {
185+
local server="$1"
186+
if [[ -n "${MCP_SERVER_API_KEYS[$server]}" ]]; then
187+
api_key_var="${MCP_SERVER_API_KEYS[$server]}"
188+
if [[ -z "${!api_key_var}" ]]; then
189+
print_warning "Skipping $server ($api_key_var not set)"
190+
return 1
191+
else
192+
print_info "$server API key is configured ($api_key_var)"
193+
fi
194+
fi
195+
return 0
196+
}
197+
198+
# Execute
199+
fix_mcp_check_api_key "exa"
200+
201+
# Verify
202+
assert_contains "$mock_output" "exa API key is configured (EXA_API_KEY)" "Should show positive message in fix-mcp-servers"
203+
'
204+
205+
# Cleanup
206+
unset EXA_API_KEY
207+
unset FIGMA_API_KEY
208+
unset ANTHROPIC_API_KEY
209+
210+
# Tests are run automatically by the test framework

0 commit comments

Comments
 (0)