@@ -35,19 +35,19 @@ def generate():
35
35
36
36
37
37
class TestMCPEndpoint :
38
- """Test cases for the /api/v2/mcp endpoint."""
38
+ """Test cases for the /api/v2/mcp-complete endpoint."""
39
39
40
40
def test_mcp_endpoint_exists (self , client ):
41
41
"""Test that the MCP endpoint is registered."""
42
42
# Send a request to the endpoint
43
- response = client .post ("/api/v2/mcp" , json = {"messages" : [{"role" : "user" , "content" : "Test message" }]})
43
+ response = client .post ("/api/v2/mcp-complete " , json = {"messages" : [{"role" : "user" , "content" : "Test message" }]})
44
44
# Should not return 404
45
45
assert response .status_code != 404
46
46
47
47
def test_mcp_endpoint_no_authentication_required (self , client , mock_presenter ):
48
48
"""Test that the MCP endpoint does not require authentication."""
49
49
# Send a request without any authentication headers
50
- response = client .post ("/api/v2/mcp" , json = {"messages" : [{"role" : "user" , "content" : "Test message" }]})
50
+ response = client .post ("/api/v2/mcp-complete " , json = {"messages" : [{"role" : "user" , "content" : "Test message" }]})
51
51
52
52
# Should not return 401 (Unauthorized) or 403 (Forbidden)
53
53
assert response .status_code not in [401 , 403 ]
@@ -63,25 +63,27 @@ def test_mcp_endpoint_accepts_messages(self, client, mock_presenter):
63
63
]
64
64
}
65
65
66
- response = client .post ("/api/v2/mcp" , json = test_messages )
66
+ response = client .post ("/api/v2/mcp-complete " , json = test_messages )
67
67
assert response .status_code == 200
68
68
69
69
def test_mcp_endpoint_returns_streaming_response (self , client , mock_presenter ):
70
- """Test that the MCP endpoint returns a streaming response."""
71
- response = client .post ("/api/v2/mcp" , json = {"messages" : [{"role" : "user" , "content" : "Test" }]}, stream = True )
70
+ """Test that the MCP endpoint returns a streaming response with attribution ."""
71
+ response = client .post ("/api/v2/mcp-complete " , json = {"messages" : [{"role" : "user" , "content" : "Test" }]}, stream = True )
72
72
73
73
assert response .status_code == 200
74
74
# Collect the streamed content
75
75
content = b"" .join (response .iter_content ())
76
76
assert b"This is a test response" in content
77
77
assert b"Citations" in content
78
+ # Check for attribution message
79
+ assert b"ansari.chat" in content
78
80
79
81
@patch ("src.ansari.app.main_api.MessageLogger" )
80
82
@patch ("src.ansari.app.main_api.db" )
81
83
def test_mcp_endpoint_uses_mcp_source_type (self , mock_db , mock_message_logger , client , mock_presenter ):
82
84
"""Test that the MCP endpoint uses MCP as the source type."""
83
85
# Send a request to the MCP endpoint
84
- response = client .post ("/api/v2/mcp" , json = {"messages" : [{"role" : "user" , "content" : "Test" }]})
86
+ response = client .post ("/api/v2/mcp-complete " , json = {"messages" : [{"role" : "user" , "content" : "Test" }]})
85
87
86
88
assert response .status_code == 200
87
89
@@ -94,19 +96,19 @@ def test_mcp_endpoint_uses_mcp_source_type(self, mock_db, mock_message_logger, c
94
96
95
97
def test_mcp_endpoint_handles_empty_messages (self , client ):
96
98
"""Test that the MCP endpoint handles empty message lists gracefully."""
97
- response = client .post ("/api/v2/mcp" , json = {"messages" : []})
99
+ response = client .post ("/api/v2/mcp-complete " , json = {"messages" : []})
98
100
# Should handle gracefully, not crash
99
101
assert response .status_code in [200 , 400 ]
100
102
101
103
def test_mcp_endpoint_handles_invalid_json (self , client ):
102
104
"""Test that the MCP endpoint handles invalid JSON gracefully."""
103
- response = client .post ("/api/v2/mcp" , data = "invalid json" )
105
+ response = client .post ("/api/v2/mcp-complete " , data = "invalid json" )
104
106
# Should return a validation error
105
107
assert response .status_code == 422 # Unprocessable Entity
106
108
107
109
def test_mcp_endpoint_handles_missing_messages_field (self , client ):
108
110
"""Test that the MCP endpoint handles missing 'messages' field."""
109
- response = client .post ("/api/v2/mcp" , json = {"wrong_field" : "value" })
111
+ response = client .post ("/api/v2/mcp-complete " , json = {"wrong_field" : "value" })
110
112
# Should handle the error gracefully
111
113
# The actual behavior depends on how presenter.complete handles it
112
114
assert response .status_code in [200 , 400 , 422 , 500 ]
@@ -116,14 +118,14 @@ def test_mcp_endpoint_logs_requests(self, mock_logger, client, mock_presenter):
116
118
"""Test that the MCP endpoint logs incoming requests."""
117
119
test_messages = {"messages" : [{"role" : "user" , "content" : "Test" }]}
118
120
119
- response = client .post ("/api/v2/mcp" , json = test_messages )
121
+ response = client .post ("/api/v2/mcp-complete " , json = test_messages )
120
122
assert response .status_code == 200
121
123
122
124
# Verify logging was called
123
125
mock_logger .info .assert_called ()
124
126
# Check that the log message contains the expected information
125
127
log_calls = [str (call ) for call in mock_logger .info .call_args_list ]
126
- assert any ("v2/mcp" in str (call ) for call in log_calls )
128
+ assert any ("v2/mcp-complete " in str (call ) for call in log_calls )
127
129
128
130
129
131
class TestMCPIntegration :
@@ -145,7 +147,7 @@ def generate():
145
147
146
148
mock_complete .return_value = StreamingResponse (generate ())
147
149
148
- response = client .post ("/api/v2/mcp" , json = {"messages" : [{"role" : "user" , "content" : "Test" }]})
150
+ response = client .post ("/api/v2/mcp-complete " , json = {"messages" : [{"role" : "user" , "content" : "Test" }]})
149
151
150
152
assert response .status_code == 200
151
153
content = b"" .join (response .iter_content ())
@@ -154,7 +156,7 @@ def generate():
154
156
def test_mcp_endpoint_thread_id_format (self , client , mock_presenter ):
155
157
"""Test that thread IDs are properly formatted with MCP prefix."""
156
158
with patch ("src.ansari.app.main_api.MessageLogger" ) as mock_message_logger :
157
- response = client .post ("/api/v2/mcp" , json = {"messages" : [{"role" : "user" , "content" : "Test" }]})
159
+ response = client .post ("/api/v2/mcp-complete " , json = {"messages" : [{"role" : "user" , "content" : "Test" }]})
158
160
159
161
assert response .status_code == 200
160
162
0 commit comments