-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_working.sh
More file actions
executable file
·207 lines (167 loc) · 6.29 KB
/
Copy pathtest_working.sh
File metadata and controls
executable file
·207 lines (167 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Test configuration
TIMEOUT=10
# Check if GITLAB_TOKEN is set
if [[ -z "$GITLAB_TOKEN" ]]; then
echo -e "${RED}GITLAB_TOKEN environment variable is not set${NC}"
echo "Please set your GitLab token: export GITLAB_TOKEN=your_token_here"
exit 1
fi
# Cleanup function
cleanup() {
echo -e "${BLUE}Cleaning up...${NC}"
# Kill server if still running
if [[ -n "$SERVER_PID" ]] && kill -0 "$SERVER_PID" 2>/dev/null; then
echo "Killing server (PID: $SERVER_PID)"
kill -TERM "$SERVER_PID" 2>/dev/null
sleep 1
if kill -0 "$SERVER_PID" 2>/dev/null; then
kill -KILL "$SERVER_PID" 2>/dev/null
fi
fi
# Remove temp files
[[ -f "$INPUT_FILE" ]] && rm -f "$INPUT_FILE"
[[ -f "$OUTPUT_FILE" ]] && rm -f "$OUTPUT_FILE"
[[ -f "$LOG_FILE" ]] && rm -f "$LOG_FILE"
echo -e "${BLUE}Cleanup completed${NC}"
}
# Set up signal handlers
trap cleanup EXIT
trap cleanup INT
trap cleanup TERM
# Helper function to test JSON-RPC message exchange
test_jsonrpc_exchange() {
local input_file="$1"
local description="$2"
echo -e "${BLUE}Testing: ${description}${NC}"
# Create temporary files
local output_file="/tmp/mcp_test_output_$$"
local log_file="/tmp/mcp_test_log_$$"
# Run server with timeout
local timeout_cmd="timeout"
if command -v gtimeout >/dev/null 2>&1; then
timeout_cmd="gtimeout"
fi
echo -e "${YELLOW}Running server with input from ${input_file}${NC}"
# Execute server and capture output
if $timeout_cmd "$TIMEOUT" go run . < "$input_file" > "$output_file" 2> "$log_file"; then
echo -e "${GREEN}Server completed successfully${NC}"
# Show output
if [[ -f "$output_file" ]] && [[ -s "$output_file" ]]; then
echo -e "${GREEN}Server responses:${NC}"
cat "$output_file" | while IFS= read -r line; do
echo "$line" | jq '.' 2>/dev/null || echo "$line"
done
else
echo -e "${YELLOW}No output received${NC}"
fi
# Show debug logs
if [[ -f "$log_file" ]] && [[ -s "$log_file" ]]; then
echo -e "${BLUE}Debug logs:${NC}"
cat "$log_file"
fi
# Cleanup temp files
[[ -f "$output_file" ]] && rm -f "$output_file"
[[ -f "$log_file" ]] && rm -f "$log_file"
return 0
else
local exit_code=$?
echo -e "${RED}Server failed or timed out (exit code: $exit_code)${NC}"
# Show any output that was generated
if [[ -f "$output_file" ]] && [[ -s "$output_file" ]]; then
echo -e "${YELLOW}Partial output:${NC}"
cat "$output_file"
fi
# Show debug logs
if [[ -f "$log_file" ]] && [[ -s "$log_file" ]]; then
echo -e "${BLUE}Debug logs:${NC}"
cat "$log_file"
fi
# Cleanup temp files
[[ -f "$output_file" ]] && rm -f "$output_file"
[[ -f "$log_file" ]] && rm -f "$log_file"
return 1
fi
}
# Main test function
run_test() {
echo -e "${GREEN}=== GitLab MCP Server Test ===${NC}"
echo "Testing MCP server functionality with proper JSON-RPC protocol"
echo
# Test 1: Initialize only
echo -e "${BLUE}=== Test 1: Initialize ===${NC}"
local init_input="/tmp/mcp_test_init_$$"
cat > "$init_input" << 'EOF'
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"tools":true},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
EOF
if test_jsonrpc_exchange "$init_input" "Initialize request"; then
echo -e "${GREEN}Initialize test passed!${NC}"
else
echo -e "${RED}Initialize test failed!${NC}"
rm -f "$init_input"
return 1
fi
rm -f "$init_input"
echo
# Test 2: Full sequence with initialize, tools/list, and tool call
echo -e "${BLUE}=== Test 2: Full MCP Sequence ===${NC}"
local full_input="/tmp/mcp_test_full_$$"
cat > "$full_input" << 'EOF'
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"tools":true},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_project_id","arguments":{"remote_url":"git@gitlab.com:namespace/project_name.git"}}}
EOF
echo -e "${YELLOW}Note: Testing with GitLab repository git@gitlab.com:namespace/project_name.git${NC}"
if test_jsonrpc_exchange "$full_input" "Full MCP sequence"; then
echo -e "${GREEN}Full sequence test passed!${NC}"
else
echo -e "${YELLOW}Full sequence test completed with some failures (expected with dummy token)${NC}"
fi
rm -f "$full_input"
echo
echo -e "${GREEN}=== Tests Completed! ===${NC}"
echo -e "${BLUE}The MCP server is responding correctly to JSON-RPC protocol messages.${NC}"
echo -e "${BLUE}For real GitLab functionality, set a valid GITLAB_TOKEN environment variable.${NC}"
return 0
}
# Check dependencies
check_dependencies() {
local missing_deps=()
# Check for required commands
command -v go >/dev/null 2>&1 || missing_deps+=("go")
command -v jq >/dev/null 2>&1 || missing_deps+=("jq")
# Check for timeout command (different on macOS)
if ! command -v timeout >/dev/null 2>&1 && ! command -v gtimeout >/dev/null 2>&1; then
missing_deps+=("timeout or gtimeout")
fi
if [[ ${#missing_deps[@]} -gt 0 ]]; then
echo -e "${RED}Missing dependencies: ${missing_deps[*]}${NC}"
echo "Please install the missing dependencies and try again."
echo "On macOS, install timeout with: brew install coreutils"
return 1
fi
return 0
}
# Main execution
main() {
# Check dependencies
if ! check_dependencies; then
exit 1
fi
# Run the test
if run_test; then
echo -e "${GREEN}All tests completed successfully!${NC}"
exit 0
else
echo -e "${RED}Some tests failed!${NC}"
exit 1
fi
}
# Run main function
main "$@"