mcpssh/test_mcp.py

86 lines
2.5 KiB
Python

#!/usr/bin/env python
"""
Test script that simulates an MCP client using the SSH server.
This helps verify that the server properly responds to MCP requests.
"""
import json
import os
import sys
import subprocess
import tempfile
# Set required environment variables for the subprocess
env = os.environ.copy()
env["MCP_SSH_HOSTNAME"] = "10.0.1.232"
env["MCP_SSH_USERNAME"] = "stwhite"
env["MCP_SSH_KEY_FILENAME"] = "~/.ssh/id_ed25519"
# Create temporary files for input/output
with tempfile.NamedTemporaryFile('w+') as input_file, tempfile.NamedTemporaryFile('w+') as output_file:
# Write an MCP request to connect to SSH server
mcp_request = {
"type": "request",
"id": "1",
"method": "ssh_connect",
"params": {}
}
input_file.write(json.dumps(mcp_request) + "\n")
input_file.flush()
# Run the MCP server process with stdin/stdout redirected
try:
print("Starting MCP SSH server...")
process = subprocess.Popen(
[sys.executable, "-m", "mcpssh"],
env=env,
stdin=open(input_file.name, 'r'),
stdout=open(output_file.name, 'w'),
stderr=subprocess.PIPE,
text=True
)
# Give it a moment to process
stderr_output = process.stderr.read()
if stderr_output:
print("Error output from server:")
print(stderr_output)
# Kill the process (we're just testing initialization)
process.terminate()
process.wait()
# Read server's response
output_file.seek(0)
response_lines = output_file.readlines()
if not response_lines:
print("No response received from server. This likely indicates an initialization issue.")
else:
for line in response_lines:
try:
response = json.loads(line.strip())
print("Server response:")
print(json.dumps(response, indent=2))
except json.JSONDecodeError:
print(f"Non-JSON response: {line.strip()}")
except Exception as e:
print(f"Error running MCP server: {e}")
print("\nTest complete.")
print("For Claude Desktop, use this configuration:")
print("""
"mcpssh": {
"command": "/Volumes/SAM2/CODE/MCP/mcpssh/venv/bin/python",
"args": [
"-m",
"mcpssh"
],
"env": {
"MCP_SSH_HOSTNAME": "10.0.1.232",
"MCP_SSH_USERNAME": "stwhite",
"MCP_SSH_KEY_FILENAME": "~/.ssh/id_ed25519"
}
}
""")