Last active
March 30, 2025 04:22
to test sse connection channel for an external model context protocol server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Colors for output | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[1;33m' | |
NC='\033[0m' # No Color | |
# Help message | |
show_help() { | |
echo "Usage: $0 [options]" | |
echo | |
echo "Options:" | |
echo " -h, --help Show this help message" | |
echo " -u, --url <url> MCP server URL (default: http://localhost:3000)" | |
echo " -t, --timeout <seconds> Request timeout in seconds (default: 10)" | |
echo | |
echo "Environment variables:" | |
echo " MCP_SERVER_URL Override MCP server URL" | |
echo " MCP_REQUEST_TIMEOUT Override request timeout" | |
echo | |
echo "Example:" | |
echo " $0 --url http://localhost:3000" | |
echo " MCP_SERVER_URL=http://localhost:3000 $0" | |
} | |
# Parse command line arguments | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-h|--help) | |
show_help | |
exit 0 | |
;; | |
-u|--url) | |
MCP_SERVER_URL="$2" | |
shift 2 | |
;; | |
-t|--timeout) | |
MCP_REQUEST_TIMEOUT="$2" | |
shift 2 | |
;; | |
*) | |
echo "Unknown option: $1" | |
show_help | |
exit 1 | |
;; | |
esac | |
done | |
# Set defaults or use environment variables | |
SERVER_URL=${MCP_SERVER_URL:-"http://localhost:3000"} | |
REQUEST_TIMEOUT=${MCP_REQUEST_TIMEOUT:-10} | |
# Initialize test results using zsh arrays | |
typeset -A test_results | |
# Function to log test result | |
log_test_result() { | |
local test_key=$1 | |
local result=$2 | |
test_results[$test_key]=$result | |
} | |
# Function to print test summary | |
print_summary() { | |
echo -e "\n${YELLOW}Test Summary:${NC}" | |
local failed=0 | |
for key in ${(k)test_results}; do | |
if [[ "${test_results[$key]}" == "pass" ]]; then | |
echo -e "${GREEN}✓ $key passed${NC}" | |
else | |
echo -e "${RED}✗ $key failed${NC}" | |
((failed++)) | |
fi | |
done | |
echo -e "\n${YELLOW}Results:${NC}" | |
local total=${#test_results} | |
local passed=$((total - failed)) | |
echo -e "Total tests: $total" | |
echo -e "${GREEN}Passed: $passed${NC}" | |
echo -e "${RED}Failed: $failed${NC}" | |
# Return overall status | |
[[ $failed -eq 0 ]] | |
} | |
# Cleanup function | |
cleanup() { | |
rm -f response.txt tools.txt tool_response.txt prompts.txt resources.txt | |
} | |
# Trap for cleanup on script exit | |
trap cleanup EXIT | |
echo -e "${YELLOW}Starting MCP SSE Connection Tests${NC}" | |
echo -e "Server URL: ${SERVER_URL}" | |
echo -e "Request timeout: ${REQUEST_TIMEOUT}s" | |
# Test 1: Basic SSE Connection | |
echo -e "\n${YELLOW}Test 1: Testing basic SSE connection${NC}" | |
if curl -s -N --max-time $REQUEST_TIMEOUT -H "Accept: text/event-stream" "${SERVER_URL}/mcp/sse" > /dev/null 2>&1; then | |
echo -e "${GREEN}✓ SSE connection established${NC}" | |
log_test_result "basic_sse" "pass" | |
else | |
echo -e "${RED}✗ Failed to establish SSE connection${NC}" | |
echo "Error details: Connection failed or timed out" | |
log_test_result "basic_sse" "fail" | |
fi | |
# Test 2: Send a chat message | |
echo -e "\n${YELLOW}Test 2: Sending a chat message${NC}" | |
curl -s --max-time $REQUEST_TIMEOUT -X POST \ | |
-H "Content-Type: application/json" \ | |
-d '{"messages":[{"role":"user","content":"Hello"}]}' \ | |
"${SERVER_URL}/mcp/chat" > response.txt 2>/dev/null | |
if [[ -s response.txt ]] && grep -q "data:" response.txt; then | |
echo -e "${GREEN}✓ Received SSE response${NC}" | |
echo "Response content:" | |
cat response.txt | |
log_test_result "chat_message" "pass" | |
else | |
echo -e "${RED}✗ No SSE response received${NC}" | |
echo "Error details: Empty response or invalid format" | |
[[ -f response.txt ]] && echo "Response content:" && cat response.txt | |
log_test_result "chat_message" "fail" | |
fi | |
# Test 3: Test MCP Tools endpoint | |
echo -e "\n${YELLOW}Test 3: Testing MCP Tools endpoint${NC}" | |
curl -s --max-time $REQUEST_TIMEOUT -X POST \ | |
-H "Content-Type: application/json" \ | |
-d '{ | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "tools/list", | |
"params": {} | |
}' \ | |
"${SERVER_URL}/tools/list" > tools.txt 2>/dev/null | |
if [[ -s tools.txt ]]; then | |
# First check if it's a valid JSON-RPC response | |
if command -v jq >/dev/null 2>&1 && jq -e '. | select(.jsonrpc == "2.0" and .id == 1)' tools.txt >/dev/null 2>&1; then | |
# Check if it's an error response | |
if jq -e '.error' tools.txt >/dev/null 2>&1; then | |
echo -e "${RED}✗ Server returned an error${NC}" | |
echo "Error details:" | |
jq '.error' tools.txt | |
log_test_result "mcp_tools" "fail" | |
# Check if it's a successful response with result | |
elif jq -e '.result' tools.txt >/dev/null 2>&1; then | |
echo -e "${GREEN}✓ Tools endpoint responded with list${NC}" | |
echo "Available tools:" | |
jq '.result' tools.txt | |
# Check pagination fields if present | |
if jq -e '.result.nextCursor' tools.txt >/dev/null 2>&1; then | |
echo -e "${GREEN}✓ Pagination supported (nextCursor field present)${NC}" | |
fi | |
log_test_result "mcp_tools" "pass" | |
else | |
echo -e "${RED}✗ Invalid response format - missing result or error${NC}" | |
echo "Response content:" | |
cat tools.txt | |
log_test_result "mcp_tools" "fail" | |
fi | |
else | |
echo -e "${RED}✗ Invalid JSON-RPC 2.0 response format${NC}" | |
echo "Response content:" | |
cat tools.txt | |
log_test_result "mcp_tools" "fail" | |
fi | |
else | |
echo -e "${RED}✗ Empty response from tools endpoint${NC}" | |
log_test_result "mcp_tools" "fail" | |
fi | |
# Test 4: Test tool execution through chat | |
echo -e "\n${YELLOW}Test 4: Testing tool execution${NC}" | |
curl -s -N --max-time $((REQUEST_TIMEOUT * 2)) -X POST \ | |
-H "Content-Type: application/json" \ | |
-d "{ | |
\"messages\":[{ | |
\"role\":\"user\", | |
\"content\":\"List the contents of the current directory\" | |
}] | |
}" \ | |
"${SERVER_URL}/mcp/chat" > tool_response.txt 2>/dev/null | |
if [[ -s tool_response.txt ]] && grep -q "data:" tool_response.txt; then | |
echo -e "${GREEN}✓ Tool execution response received${NC}" | |
echo "Tool execution response:" | |
cat tool_response.txt | |
log_test_result "tool_execution" "pass" | |
else | |
echo -e "${RED}✗ No tool execution response received${NC}" | |
echo "Error details: Empty response or invalid format" | |
[[ -f tool_response.txt ]] && echo "Response content:" && cat tool_response.txt | |
log_test_result "tool_execution" "fail" | |
fi | |
# Test 5: Test prompts/list endpoint | |
echo -e "\n${YELLOW}Test 5: Testing prompts/list endpoint${NC}" | |
curl -s --max-time $REQUEST_TIMEOUT -X POST \ | |
-H "Content-Type: application/json" \ | |
-d '{ | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "prompts/list", | |
"params": {} | |
}' \ | |
"${SERVER_URL}/prompts/list" > prompts.txt 2>/dev/null | |
if [[ -s prompts.txt ]]; then | |
if command -v jq >/dev/null 2>&1 && jq -e '.result' prompts.txt >/dev/null 2>&1; then | |
echo -e "${GREEN}✓ Prompts endpoint responded with list${NC}" | |
echo "Available prompts:" | |
jq '.result' prompts.txt | |
# Check if listChanged field is present | |
if jq -e '.result.listChanged' prompts.txt >/dev/null 2>&1; then | |
echo -e "${GREEN}✓ listChanged field is present${NC}" | |
log_test_result "prompts_list" "pass" | |
else | |
echo -e "${RED}✗ listChanged field is missing${NC}" | |
log_test_result "prompts_list" "fail" | |
fi | |
else | |
echo -e "${RED}✗ Invalid JSON-RPC response format${NC}" | |
echo "Response content:" | |
cat prompts.txt | |
log_test_result "prompts_list" "fail" | |
fi | |
else | |
echo -e "${RED}✗ Empty response from prompts endpoint${NC}" | |
log_test_result "prompts_list" "fail" | |
fi | |
# Test 6: Test resources/list endpoint | |
echo -e "\n${YELLOW}Test 6: Testing resources/list endpoint${NC}" | |
curl -s --max-time $REQUEST_TIMEOUT -X POST \ | |
-H "Content-Type: application/json" \ | |
-d '{ | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "resources/list", | |
"params": {} | |
}' \ | |
"${SERVER_URL}/resources/list" > resources.txt 2>/dev/null | |
if [[ -s resources.txt ]]; then | |
# First check if it's a valid JSON-RPC response | |
if command -v jq >/dev/null 2>&1 && jq -e '. | select(.jsonrpc == "2.0" and .id == 1)' resources.txt >/dev/null 2>&1; then | |
# Check if it's an error response | |
if jq -e '.error' resources.txt >/dev/null 2>&1; then | |
echo -e "${RED}✗ Server returned an error${NC}" | |
echo "Error details:" | |
jq '.error' resources.txt | |
log_test_result "resources_list" "fail" | |
# Check if it's a successful response with result | |
elif jq -e '.result' resources.txt >/dev/null 2>&1; then | |
echo -e "${GREEN}✓ Resources endpoint responded with list${NC}" | |
echo "Available resources:" | |
jq '.result' resources.txt | |
# Check pagination fields if present | |
if jq -e '.result.nextCursor' resources.txt >/dev/null 2>&1; then | |
echo -e "${GREEN}✓ Pagination supported (nextCursor field present)${NC}" | |
fi | |
log_test_result "resources_list" "pass" | |
else | |
echo -e "${RED}✗ Invalid response format - missing result or error${NC}" | |
echo "Response content:" | |
cat resources.txt | |
log_test_result "resources_list" "fail" | |
fi | |
else | |
echo -e "${RED}✗ Invalid JSON-RPC 2.0 response format${NC}" | |
echo "Response content:" | |
cat resources.txt | |
log_test_result "resources_list" "fail" | |
fi | |
else | |
echo -e "${RED}✗ Empty response from resources endpoint${NC}" | |
log_test_result "resources_list" "fail" | |
fi | |
# Print test summary | |
print_summary | |
# Show URLs used | |
echo -e "\n${YELLOW}URLs Used:${NC}" | |
echo -e "SSE Connection: ${SERVER_URL}/mcp/sse" | |
echo -e "Chat Endpoint: ${SERVER_URL}/mcp/chat" | |
echo -e "Tools Endpoint: ${SERVER_URL}/tools/list" | |
echo -e "Prompts Endpoint: ${SERVER_URL}/prompts/list" | |
echo -e "Resources Endpoint: ${SERVER_URL}/resources/list" | |
# Exit with overall status | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment