|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Test backward compatibility for tool invocation after PR #746.""" |
| 3 | + |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from fastapi.testclient import TestClient |
| 8 | +from sqlalchemy.orm import Session |
| 9 | + |
| 10 | +from mcpgateway.main import app |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def client(): |
| 15 | + """Create a test client.""" |
| 16 | + return TestClient(app) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_db(): |
| 21 | + """Create a mock database session.""" |
| 22 | + return MagicMock(spec=Session) |
| 23 | + |
| 24 | + |
| 25 | +class TestRPCBackwardCompatibility: |
| 26 | + """Test backward compatibility for RPC tool invocation.""" |
| 27 | + |
| 28 | + def test_old_format_tool_invocation_with_backward_compatibility(self, client, mock_db): |
| 29 | + """Test that old format (direct tool name as method) still works with backward compatibility.""" |
| 30 | + with patch("mcpgateway.config.settings.auth_required", False): |
| 31 | + with patch("mcpgateway.main.get_db", return_value=mock_db): |
| 32 | + with patch("mcpgateway.main.tool_service.invoke_tool", new_callable=AsyncMock) as mock_invoke: |
| 33 | + mock_invoke.return_value = {"result": "success", "data": "test data from old format"} |
| 34 | + |
| 35 | + # Old format: tool name directly as method |
| 36 | + request_body = {"jsonrpc": "2.0", "method": "my_custom_tool", "params": {"query": "test query", "limit": 10}, "id": 123} |
| 37 | + |
| 38 | + response = client.post("/rpc", json=request_body) |
| 39 | + |
| 40 | + assert response.status_code == 200 |
| 41 | + result = response.json() |
| 42 | + assert result["jsonrpc"] == "2.0" |
| 43 | + assert "result" in result |
| 44 | + assert result["result"]["result"] == "success" |
| 45 | + assert result["result"]["data"] == "test data from old format" |
| 46 | + assert result["id"] == 123 |
| 47 | + |
| 48 | + # Verify the tool was invoked with correct parameters |
| 49 | + mock_invoke.assert_called_once() |
| 50 | + call_args = mock_invoke.call_args |
| 51 | + assert call_args.kwargs["name"] == "my_custom_tool" |
| 52 | + assert call_args.kwargs["arguments"] == {"query": "test query", "limit": 10} |
| 53 | + |
| 54 | + def test_new_format_tool_invocation_still_works(self, client, mock_db): |
| 55 | + """Test that new format (tools/call method) continues to work.""" |
| 56 | + with patch("mcpgateway.config.settings.auth_required", False): |
| 57 | + with patch("mcpgateway.main.get_db", return_value=mock_db): |
| 58 | + with patch("mcpgateway.main.tool_service.invoke_tool", new_callable=AsyncMock) as mock_invoke: |
| 59 | + mock_invoke.return_value = {"result": "success", "data": "test data from new format"} |
| 60 | + |
| 61 | + # New format: tools/call method |
| 62 | + request_body = {"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "my_custom_tool", "arguments": {"query": "test query", "limit": 10}}, "id": 456} |
| 63 | + |
| 64 | + response = client.post("/rpc", json=request_body) |
| 65 | + |
| 66 | + assert response.status_code == 200 |
| 67 | + result = response.json() |
| 68 | + assert result["jsonrpc"] == "2.0" |
| 69 | + assert "result" in result |
| 70 | + assert result["result"]["result"] == "success" |
| 71 | + assert result["result"]["data"] == "test data from new format" |
| 72 | + assert result["id"] == 456 |
| 73 | + |
| 74 | + # Verify the tool was invoked with correct parameters |
| 75 | + mock_invoke.assert_called_once() |
| 76 | + call_args = mock_invoke.call_args |
| 77 | + assert call_args.kwargs["name"] == "my_custom_tool" |
| 78 | + assert call_args.kwargs["arguments"] == {"query": "test query", "limit": 10} |
| 79 | + |
| 80 | + def test_both_formats_invoke_same_tool(self, client, mock_db): |
| 81 | + """Test that both old and new formats can invoke the same tool successfully.""" |
| 82 | + with patch("mcpgateway.config.settings.auth_required", False): |
| 83 | + with patch("mcpgateway.main.get_db", return_value=mock_db): |
| 84 | + with patch("mcpgateway.main.tool_service.invoke_tool", new_callable=AsyncMock) as mock_invoke: |
| 85 | + mock_invoke.return_value = {"result": "success"} |
| 86 | + |
| 87 | + # Test old format |
| 88 | + old_format_request = {"jsonrpc": "2.0", "method": "search_tool", "params": {"query": "old format"}, "id": 1} |
| 89 | + |
| 90 | + response_old = client.post("/rpc", json=old_format_request) |
| 91 | + assert response_old.status_code == 200 |
| 92 | + |
| 93 | + # Reset mock |
| 94 | + mock_invoke.reset_mock() |
| 95 | + |
| 96 | + # Test new format |
| 97 | + new_format_request = {"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "search_tool", "arguments": {"query": "new format"}}, "id": 2} |
| 98 | + |
| 99 | + response_new = client.post("/rpc", json=new_format_request) |
| 100 | + assert response_new.status_code == 200 |
| 101 | + |
| 102 | + # Both should have invoked the tool |
| 103 | + assert mock_invoke.call_count == 1 |
| 104 | + call_args = mock_invoke.call_args |
| 105 | + assert call_args.kwargs["name"] == "search_tool" |
| 106 | + assert call_args.kwargs["arguments"]["query"] == "new format" |
| 107 | + |
| 108 | + def test_invalid_method_still_returns_error(self, client, mock_db): |
| 109 | + """Test that truly invalid methods still return an error.""" |
| 110 | + with patch("mcpgateway.config.settings.auth_required", False): |
| 111 | + with patch("mcpgateway.main.get_db", return_value=mock_db): |
| 112 | + with patch("mcpgateway.main.tool_service.invoke_tool", new_callable=AsyncMock) as mock_invoke: |
| 113 | + # Simulate tool not found |
| 114 | + mock_invoke.side_effect = ValueError("Tool not found") |
| 115 | + |
| 116 | + with patch("mcpgateway.main.gateway_service.forward_request", new_callable=AsyncMock) as mock_forward: |
| 117 | + # Simulate gateway forward also failing |
| 118 | + mock_forward.side_effect = ValueError("Not a gateway method") |
| 119 | + |
| 120 | + request_body = {"jsonrpc": "2.0", "method": "completely_invalid_method", "params": {}, "id": 999} |
| 121 | + |
| 122 | + response = client.post("/rpc", json=request_body) |
| 123 | + |
| 124 | + assert response.status_code == 200 |
| 125 | + result = response.json() |
| 126 | + assert result["jsonrpc"] == "2.0" |
| 127 | + assert "error" in result |
| 128 | + assert result["error"]["code"] == -32000 |
| 129 | + assert result["error"]["message"] == "Invalid method" |
| 130 | + assert result["id"] == 999 |
0 commit comments