Skip to main content
Glama

MCP Auth Server

test_step6.shโ€ข8.56 kB
#!/bin/bash # Test script for Step 6: JWT Token Validation echo "๐Ÿงช Testing Step 6: JWT Token Validation" echo "========================================" # Function to cleanup background processes cleanup() { echo "๐Ÿงน Cleaning up..." if [ ! -z "$SERVER_PID" ]; then kill $SERVER_PID 2>/dev/null wait $SERVER_PID 2>/dev/null fi exit 0 } # Set trap to cleanup on script exit trap cleanup EXIT INT TERM # Check if public key exists, if not generate one if [ ! -f "mcp_public_key.pem" ]; then echo "๐Ÿ”‘ No public key found, generating key pair..." uv run python generate_token.py --generate-keys if [ $? -ne 0 ]; then echo "โŒ Failed to generate key pair" exit 1 fi fi # Generate test tokens echo "๐Ÿ”‘ Generating test tokens..." uv run python generate_token.py --username alice --scopes mcp:read,mcp:tools uv run python generate_token.py --username admin --scopes mcp:read,mcp:tools,mcp:prompts # Extract tokens from generated files ALICE_TOKEN=$(python -c "import json; print(json.load(open('token_alice.json'))['token'])") ADMIN_TOKEN=$(python -c "import json; print(json.load(open('token_admin.json'))['token'])") if [ -z "$ALICE_TOKEN" ] || [ -z "$ADMIN_TOKEN" ]; then echo "โŒ Failed to extract tokens" exit 1 fi echo "โœ… Test tokens generated successfully" # Start the server in background echo "๐Ÿš€ Starting Step 6 server..." uv run step6 & SERVER_PID=$! # Wait for server to start echo "โณ Waiting for server to start..." sleep 3 # Test health endpoint echo "๐Ÿ” Testing health endpoint..." HEALTH_RESPONSE=$(curl -s http://localhost:9000/health) if [ $? -eq 0 ]; then echo "โœ… Health endpoint test passed!" echo "๐Ÿ“„ Response: $HEALTH_RESPONSE" # Check if auth is required if echo "$HEALTH_RESPONSE" | grep -q '"auth_required":true'; then echo "โœ… Authentication is required!" else echo "โŒ Authentication requirement not indicated" exit 1 fi else echo "โŒ Health endpoint test failed!" exit 1 fi # Test JWKS endpoint echo "๐Ÿ” Testing JWKS endpoint..." JWKS_RESPONSE=$(curl -s http://localhost:9000/.well-known/jwks.json) if [ $? -eq 0 ]; then echo "โœ… JWKS endpoint test passed!" echo "๐Ÿ“„ Response: $JWKS_RESPONSE" # Check if JWKS contains keys if echo "$JWKS_RESPONSE" | grep -q '"keys"'; then echo "โœ… JWKS contains public key!" else echo "โŒ JWKS does not contain keys" exit 1 fi else echo "โŒ JWKS endpoint test failed!" exit 1 fi # Test unauthenticated MCP request (should fail) echo "๐Ÿ” Testing unauthenticated MCP request..." UNAUTH_RESPONSE=$(curl -s -X POST http://localhost:9000/mcp \ -H "Content-Type: application/json" \ -d '{"jsonrpc": "2.0", "id": 1, "method": "initialize"}') if [ $? -eq 0 ]; then echo "โœ… Unauthenticated request test passed!" echo "๐Ÿ“„ Response: $UNAUTH_RESPONSE" # Check if response contains 401 error if echo "$UNAUTH_RESPONSE" | grep -q '"detail":"Authorization header missing"'; then echo "โœ… Properly rejects unauthenticated requests!" else echo "โŒ Did not properly reject unauthenticated request" exit 1 fi else echo "โŒ Unauthenticated request test failed!" exit 1 fi # Test authenticated MCP initialize with Alice token echo "๐Ÿ” Testing authenticated MCP initialize (Alice)..." AUTH_INIT_RESPONSE=$(curl -s -X POST http://localhost:9000/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ALICE_TOKEN" \ -d '{"jsonrpc": "2.0", "id": 1, "method": "initialize"}') if [ $? -eq 0 ]; then echo "โœ… Authenticated initialize test passed!" echo "๐Ÿ“„ Response: $AUTH_INIT_RESPONSE" # Check if response contains user info if echo "$AUTH_INIT_RESPONSE" | grep -q '"authenticatedUser":"alice"'; then echo "โœ… Initialize response includes authenticated user!" else echo "โŒ Initialize response missing authenticated user" exit 1 fi else echo "โŒ Authenticated initialize test failed!" exit 1 fi # Test authenticated tools/list with Alice token echo "๐Ÿ” Testing authenticated tools/list (Alice)..." AUTH_TOOLS_RESPONSE=$(curl -s -X POST http://localhost:9000/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ALICE_TOKEN" \ -d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}') if [ $? -eq 0 ]; then echo "โœ… Authenticated tools/list test passed!" echo "๐Ÿ“„ Response: $AUTH_TOOLS_RESPONSE" # Check if response contains tools if echo "$AUTH_TOOLS_RESPONSE" | grep -q '"name":"echo"'; then echo "โœ… Echo tool is listed correctly for authenticated user!" else echo "โŒ Echo tool not found in authenticated response!" exit 1 fi else echo "โŒ Authenticated tools/list test failed!" exit 1 fi # Test authenticated tools/call with Alice token echo "๐Ÿ” Testing authenticated tools/call (Alice)..." AUTH_CALL_RESPONSE=$(curl -s -X POST http://localhost:9000/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ALICE_TOKEN" \ -d '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "echo", "arguments": {"message": "Hello Alice", "repeat_count": 2}}}') if [ $? -eq 0 ]; then echo "โœ… Authenticated tools/call test passed!" echo "๐Ÿ“„ Response: $AUTH_CALL_RESPONSE" # Check if response contains the echoed message if echo "$AUTH_CALL_RESPONSE" | grep -q 'Hello AliceHello Alice'; then echo "โœ… Echo tool works correctly for authenticated user!" else echo "โŒ Echo tool response is incorrect for authenticated user!" exit 1 fi else echo "โŒ Authenticated tools/call test failed!" exit 1 fi # Test authenticated ping with Alice token echo "๐Ÿ” Testing authenticated ping (Alice)..." AUTH_PING_RESPONSE=$(curl -s -X POST http://localhost:9000/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ALICE_TOKEN" \ -d '{"jsonrpc": "2.0", "id": 4, "method": "ping"}') if [ $? -eq 0 ]; then echo "โœ… Authenticated ping test passed!" echo "๐Ÿ“„ Response: $AUTH_PING_RESPONSE" # Check if response contains user info if echo "$AUTH_PING_RESPONSE" | grep -q '"user":"alice"' && echo "$AUTH_PING_RESPONSE" | grep -q '"authenticated":true'; then echo "โœ… Ping response includes authenticated user info!" else echo "โŒ Ping response missing authenticated user info" exit 1 fi else echo "โŒ Authenticated ping test failed!" exit 1 fi # Test authenticated request with Admin token echo "๐Ÿ” Testing authenticated request (Admin)..." ADMIN_RESPONSE=$(curl -s -X POST http://localhost:9000/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ADMIN_TOKEN" \ -d '{"jsonrpc": "2.0", "id": 5, "method": "initialize"}') if [ $? -eq 0 ]; then echo "โœ… Admin authenticated request test passed!" echo "๐Ÿ“„ Response: $ADMIN_RESPONSE" # Check if response contains admin user info if echo "$ADMIN_RESPONSE" | grep -q '"authenticatedUser":"admin"'; then echo "โœ… Admin user is properly authenticated!" else echo "โŒ Admin user not properly authenticated" exit 1 fi else echo "โŒ Admin authenticated request test failed!" exit 1 fi # Test invalid token (should fail) echo "๐Ÿ” Testing invalid token..." INVALID_RESPONSE=$(curl -s -X POST http://localhost:9000/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer invalid.token.here" \ -d '{"jsonrpc": "2.0", "id": 6, "method": "initialize"}') if [ $? -eq 0 ]; then echo "โœ… Invalid token test passed!" echo "๐Ÿ“„ Response: $INVALID_RESPONSE" # Check if response contains error if echo "$INVALID_RESPONSE" | grep -q '"detail":"Invalid token"'; then echo "โœ… Properly rejects invalid tokens!" else echo "โŒ Did not properly reject invalid token" exit 1 fi else echo "โŒ Invalid token test failed!" exit 1 fi echo "" echo "๐ŸŽ‰ Step 6 tests completed successfully!" echo "โœ… JWT token validation is working" echo "โœ… Authentication is enforced for MCP requests" echo "โœ… Authenticated users can access all MCP functionality" echo "โœ… User context is included in responses" echo "โœ… Invalid tokens are properly rejected" echo "โœ… WWW-Authenticate headers are set correctly" echo "โœ… Ready for next step: OAuth 2.0 metadata"

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/christian-posta/mcp-auth-step-by-step'

If you have feedback or need assistance with the MCP directory API, please join our Discord server