|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from llama_stack_api import ImageContentItem, TextContentItem, URL, _URLOrData |
| 5 | +from llama_stack_api.openai_responses import ( |
| 6 | + OpenAIResponseInputToolFileSearch as InputToolFileSearch, |
| 7 | + OpenAIResponseInputToolMCP as InputToolMCP, |
| 8 | +) |
5 | 9 |
|
6 | 10 | from pydantic import AnyUrl, ValidationError |
7 | 11 | from pytest_mock import MockerFixture |
8 | 12 |
|
9 | 13 | from utils.types import ( |
10 | 14 | GraniteToolParser, |
11 | 15 | ReferencedDocument, |
| 16 | + ResponsesApiParams, |
12 | 17 | ToolCallSummary, |
13 | 18 | ToolResultSummary, |
14 | 19 | content_to_str, |
@@ -194,3 +199,130 @@ def test_constructor_partial_fields(self) -> None: |
194 | 199 | doc = ReferencedDocument(doc_title="Test Title") |
195 | 200 | assert doc.doc_url is None |
196 | 201 | assert doc.doc_title == "Test Title" |
| 202 | + |
| 203 | + |
| 204 | +class TestResponsesApiParamsModelDump: |
| 205 | + """Tests for ResponsesApiParams.model_dump() MCP authorization fix. |
| 206 | +
|
| 207 | + Regression tests for LCORE-1414 / GitHub issue #1269: llama-stack-api's |
| 208 | + InputToolMCP.authorization has Field(exclude=True), causing the base |
| 209 | + model_dump() to silently strip authorization tokens. |
| 210 | + """ |
| 211 | + |
| 212 | + def _make_params(self, tools: list) -> ResponsesApiParams: |
| 213 | + """Build minimal ResponsesApiParams with given tools.""" |
| 214 | + return ResponsesApiParams( |
| 215 | + input="test question", |
| 216 | + model="provider/model", |
| 217 | + conversation="conv-id", |
| 218 | + store=False, |
| 219 | + stream=False, |
| 220 | + tools=tools, |
| 221 | + ) |
| 222 | + |
| 223 | + def test_mcp_authorization_survives_model_dump(self) -> None: |
| 224 | + """Test that MCP authorization is re-injected after model_dump().""" |
| 225 | + tool = InputToolMCP( |
| 226 | + server_label="auth-server", |
| 227 | + server_url="http://localhost:3000", |
| 228 | + require_approval="never", |
| 229 | + authorization="my-secret-token", |
| 230 | + ) |
| 231 | + assert tool.authorization == "my-secret-token" |
| 232 | + assert "authorization" not in tool.model_dump() |
| 233 | + |
| 234 | + params = self._make_params([tool]) |
| 235 | + dumped = params.model_dump(exclude_none=True) |
| 236 | + assert dumped["tools"][0]["authorization"] == "my-secret-token" |
| 237 | + |
| 238 | + def test_mcp_authorization_none_not_injected(self) -> None: |
| 239 | + """Test that None authorization is not added to the dump.""" |
| 240 | + tool = InputToolMCP( |
| 241 | + server_label="no-auth-server", |
| 242 | + server_url="http://localhost:3000", |
| 243 | + require_approval="never", |
| 244 | + ) |
| 245 | + params = self._make_params([tool]) |
| 246 | + dumped = params.model_dump(exclude_none=True) |
| 247 | + assert "authorization" not in dumped["tools"][0] |
| 248 | + |
| 249 | + def test_non_mcp_tools_unaffected(self) -> None: |
| 250 | + """Test that non-MCP tools are not modified by the override.""" |
| 251 | + tool = InputToolFileSearch( |
| 252 | + type="file_search", |
| 253 | + vector_store_ids=["vs-1"], |
| 254 | + ) |
| 255 | + params = self._make_params([tool]) |
| 256 | + dumped = params.model_dump(exclude_none=True) |
| 257 | + assert "authorization" not in dumped["tools"][0] |
| 258 | + |
| 259 | + def test_mixed_tools_only_mcp_gets_authorization(self) -> None: |
| 260 | + """Test mixed tool list: only MCP tools get authorization re-injected.""" |
| 261 | + mcp_tool = InputToolMCP( |
| 262 | + server_label="auth-server", |
| 263 | + server_url="http://localhost:3000", |
| 264 | + require_approval="never", |
| 265 | + authorization="secret", |
| 266 | + ) |
| 267 | + file_tool = InputToolFileSearch( |
| 268 | + type="file_search", |
| 269 | + vector_store_ids=["vs-1"], |
| 270 | + ) |
| 271 | + params = self._make_params([file_tool, mcp_tool]) |
| 272 | + dumped = params.model_dump(exclude_none=True) |
| 273 | + |
| 274 | + assert "authorization" not in dumped["tools"][0] |
| 275 | + assert dumped["tools"][1]["authorization"] == "secret" |
| 276 | + |
| 277 | + def test_multiple_mcp_tools_each_preserves_authorization(self) -> None: |
| 278 | + """Test that each MCP tool gets its own authorization re-injected.""" |
| 279 | + tool_a = InputToolMCP( |
| 280 | + server_label="server-a", |
| 281 | + server_url="http://a:3000", |
| 282 | + require_approval="never", |
| 283 | + authorization="token-a", |
| 284 | + ) |
| 285 | + tool_b = InputToolMCP( |
| 286 | + server_label="server-b", |
| 287 | + server_url="http://b:3000", |
| 288 | + require_approval="never", |
| 289 | + authorization="token-b", |
| 290 | + ) |
| 291 | + params = self._make_params([tool_a, tool_b]) |
| 292 | + dumped = params.model_dump(exclude_none=True) |
| 293 | + |
| 294 | + assert dumped["tools"][0]["authorization"] == "token-a" |
| 295 | + assert dumped["tools"][1]["authorization"] == "token-b" |
| 296 | + |
| 297 | + def test_exclude_changing_tool_list_shape_skips_reinjection(self) -> None: |
| 298 | + """Test that exclude removing tool indices does not mis-assign authorization.""" |
| 299 | + tool_a = InputToolMCP( |
| 300 | + server_label="server-a", |
| 301 | + server_url="http://a:3000", |
| 302 | + require_approval="never", |
| 303 | + authorization="token-a", |
| 304 | + ) |
| 305 | + tool_b = InputToolMCP( |
| 306 | + server_label="server-b", |
| 307 | + server_url="http://b:3000", |
| 308 | + require_approval="never", |
| 309 | + authorization="token-b", |
| 310 | + ) |
| 311 | + params = self._make_params([tool_a, tool_b]) |
| 312 | + dumped = params.model_dump(exclude={"tools": {0}}) |
| 313 | + assert len(dumped["tools"]) == 1 |
| 314 | + assert dumped["tools"][0]["server_label"] == "server-b" |
| 315 | + assert "authorization" not in dumped["tools"][0] |
| 316 | + |
| 317 | + def test_no_tools_does_not_error(self) -> None: |
| 318 | + """Test that model_dump() works when tools is None.""" |
| 319 | + params = ResponsesApiParams( |
| 320 | + input="test", |
| 321 | + model="provider/model", |
| 322 | + conversation="conv-id", |
| 323 | + store=False, |
| 324 | + stream=False, |
| 325 | + tools=None, |
| 326 | + ) |
| 327 | + dumped = params.model_dump(exclude_none=True) |
| 328 | + assert "tools" not in dumped |
0 commit comments