Spaces:
Running
Running
File size: 31,085 Bytes
f7f3b5c 8430481 3959209 f7f3b5c 1a49d90 f7f3b5c b6176a5 f7f3b5c 1a49d90 f7f3b5c b6176a5 f7f3b5c 1a49d90 f7f3b5c b6176a5 f7f3b5c b6176a5 f7f3b5c 1a49d90 3476d1b b6176a5 3476d1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | import pytest
from starlette import status
from starlette.testclient import TestClient
from fastmcp import FastMCP
from fastmcp.contrib.component_manager import set_up_component_manager
from fastmcp.server.auth.providers.jwt import JWTVerifier, RSAKeyPair
class TestComponentManagementRoutes:
"""Test the component management routes for tools, resources, and prompts."""
@pytest.fixture
def mounted_mcp(self):
"""Create a FastMCP server with a mounted sub-server and a tool, resource, and prompt on the sub-server."""
mounted_mcp = FastMCP("SubServer")
@mounted_mcp.tool()
def mounted_tool() -> str:
"""Test tool for tool management routes."""
return "mounted_tool_result"
@mounted_mcp.resource("data://mounted_resource")
def mounted_resource() -> str:
"""Test resource for tool management routes."""
return "mounted_resource_result"
# Add a test resource
@mounted_mcp.resource("data://mounted_resource/{id}")
def test_template(id: str) -> dict:
"""Test template for tool management routes."""
return {"id": id, "value": "data"}
@mounted_mcp.prompt()
def mounted_prompt() -> str:
"""Test prompt for tool management routes."""
return "mounted_prompt_result"
return mounted_mcp
@pytest.fixture
def mcp(self, mounted_mcp):
"""Create a FastMCP server with test tools, resources, and prompts."""
mcp = FastMCP("TestServer")
mcp.mount(mounted_mcp, prefix="sub")
set_up_component_manager(server=mcp)
# Add a test tool
@mcp.tool
def test_tool() -> str:
"""Test tool for tool management routes."""
return "test_tool_result"
# Add a test resource
@mcp.resource("data://test_resource")
def test_resource() -> str:
"""Test resource for tool management routes."""
return "test_resource_result"
# Add a test resource
@mcp.resource("data://test_resource/{id}")
def test_template(id: str) -> dict:
"""Test template for tool management routes."""
return {"id": id, "value": "data"}
# Add a test prompt
@mcp.prompt
def test_prompt() -> str:
"""Test prompt for tool management routes."""
return "test_prompt_result"
return mcp
@pytest.fixture
def client(self, mcp):
"""Create a test client for the FastMCP server."""
return TestClient(mcp.http_app())
async def test_enable_tool_route(self, client, mcp):
"""Test enabling a tool via the HTTP route."""
# First disable the tool
tool = await mcp._tool_manager.get_tool("test_tool")
tool.enabled = False
# Enable the tool via the HTTP route
response = client.post("/tools/test_tool/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Enabled tool: test_tool"}
# Verify the tool is enabled
tool = await mcp._tool_manager.get_tool("test_tool")
assert tool.enabled is True
async def test_disable_tool_route(self, client, mcp):
"""Test disabling a tool via the HTTP route."""
# First ensure the tool is enabled
tool = await mcp._tool_manager.get_tool("test_tool")
tool.enabled = True
# Disable the tool via the HTTP route
response = client.post("/tools/test_tool/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Disabled tool: test_tool"}
# Verify the tool is disabled
tool = await mcp._tool_manager.get_tool("test_tool")
assert tool.enabled is False
async def test_enable_resource_route(self, client, mcp):
"""Test enabling a resource via the HTTP route."""
# First disable the resource
resource = await mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = False
# Enable the resource via the HTTP route
response = client.post("/resources/data://test_resource/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Enabled resource: data://test_resource"}
# Verify the resource is enabled
resource = await mcp._resource_manager.get_resource("data://test_resource")
assert resource.enabled is True
async def test_disable_resource_route(self, client, mcp):
"""Test disabling a resource via the HTTP route."""
# First ensure the resource is enabled
resource = await mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = True
# Disable the resource via the HTTP route
response = client.post("/resources/data://test_resource/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Disabled resource: data://test_resource"}
# Verify the resource is disabled
resource = await mcp._resource_manager.get_resource("data://test_resource")
assert resource.enabled is False
async def test_enable_template_route(self, client, mcp):
"""Test enabling a resource on a mounted server via the parent server's HTTP route."""
key = "data://test_resource/{id}"
resource = mcp._resource_manager._templates[key]
resource.enabled = False
response = client.post("/resources/data://test_resource/{id}/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"message": "Enabled resource: data://test_resource/{id}"
}
assert resource.enabled is True
async def test_disable_template_route(self, client, mcp):
"""Test disabling a resource on a mounted server via the parent server's HTTP route."""
key = "data://test_resource/{id}"
resource = mcp._resource_manager._templates[key]
resource.enabled = True
response = client.post("/resources/data://test_resource/{id}/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"message": "Disabled resource: data://test_resource/{id}"
}
assert resource.enabled is False
async def test_enable_prompt_route(self, client, mcp):
"""Test enabling a prompt via the HTTP route."""
# First disable the prompt
prompt = await mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = False
# Enable the prompt via the HTTP route
response = client.post("/prompts/test_prompt/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Enabled prompt: test_prompt"}
# Verify the prompt is enabled
prompt = await mcp._prompt_manager.get_prompt("test_prompt")
assert prompt.enabled is True
async def test_disable_prompt_route(self, client, mcp):
"""Test disabling a prompt via the HTTP route."""
# First ensure the prompt is enabled
prompt = await mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = True
# Disable the prompt via the HTTP route
response = client.post("/prompts/test_prompt/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Disabled prompt: test_prompt"}
# Verify the prompt is disabled
prompt = await mcp._prompt_manager.get_prompt("test_prompt")
assert prompt.enabled is False
async def test_enable_tool_route_on_mounted_server(self, client, mounted_mcp):
"""Test enabling a tool on a mounted server via the parent server's HTTP route."""
# Disable the tool on the sub-server
sub_tool = await mounted_mcp._tool_manager.get_tool("mounted_tool")
sub_tool.enabled = False
# Enable via parent
response = client.post("/tools/sub_mounted_tool/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Enabled tool: sub_mounted_tool"}
# Confirm disabled on sub-server
assert sub_tool.enabled is True
async def test_disable_tool_route_on_mounted_server(self, client, mounted_mcp):
"""Test disabling a tool on a mounted server via the parent server's HTTP route."""
# Enable the tool on the sub-server
sub_tool = await mounted_mcp._tool_manager.get_tool("mounted_tool")
sub_tool.enabled = True
# Disable via parent
response = client.post("/tools/sub_mounted_tool/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Disabled tool: sub_mounted_tool"}
# Confirm disabled on sub-server
assert sub_tool.enabled is False
async def test_enable_resource_route_on_mounted_server(self, client, mounted_mcp):
"""Test enabling a resource on a mounted server via the parent server's HTTP route."""
resource = await mounted_mcp._resource_manager.get_resource(
"data://mounted_resource"
)
resource.enabled = False
response = client.post("/resources/data://sub/mounted_resource/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"message": "Enabled resource: data://sub/mounted_resource"
}
resource = await mounted_mcp._resource_manager.get_resource(
"data://mounted_resource"
)
assert resource.enabled is True
async def test_disable_resource_route_on_mounted_server(self, client, mounted_mcp):
"""Test disabling a resource on a mounted server via the parent server's HTTP route."""
resource = await mounted_mcp._resource_manager.get_resource(
"data://mounted_resource"
)
resource.enabled = True
response = client.post("/resources/data://sub/mounted_resource/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"message": "Disabled resource: data://sub/mounted_resource"
}
resource = await mounted_mcp._resource_manager.get_resource(
"data://mounted_resource"
)
assert resource.enabled is False
async def test_enable_template_route_on_mounted_server(self, client, mounted_mcp):
"""Test enabling a resource on a mounted server via the parent server's HTTP route."""
key = "data://mounted_resource/{id}"
resource = mounted_mcp._resource_manager._templates[key]
resource.enabled = False
response = client.post("/resources/data://sub/mounted_resource/{id}/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"message": "Enabled resource: data://sub/mounted_resource/{id}"
}
assert resource.enabled is True
async def test_disable_template_route_on_mounted_server(self, client, mounted_mcp):
"""Test disabling a resource on a mounted server via the parent server's HTTP route."""
key = "data://mounted_resource/{id}"
resource = mounted_mcp._resource_manager._templates[key]
resource.enabled = True
response = client.post("/resources/data://sub/mounted_resource/{id}/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"message": "Disabled resource: data://sub/mounted_resource/{id}"
}
assert resource.enabled is False
async def test_enable_prompt_route_on_mounted_server(self, client, mounted_mcp):
"""Test enabling a prompt on a mounted server via the parent server's HTTP route."""
prompt = await mounted_mcp._prompt_manager.get_prompt("mounted_prompt")
prompt.enabled = False
response = client.post("/prompts/sub_mounted_prompt/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Enabled prompt: sub_mounted_prompt"}
prompt = await mounted_mcp._prompt_manager.get_prompt("mounted_prompt")
assert prompt.enabled is True
async def test_disable_prompt_route_on_mounted_server(self, client, mounted_mcp):
"""Test disabling a prompt on a mounted server via the parent server's HTTP route."""
prompt = await mounted_mcp._prompt_manager.get_prompt("mounted_prompt")
prompt.enabled = True
response = client.post("/prompts/sub_mounted_prompt/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Disabled prompt: sub_mounted_prompt"}
prompt = await mounted_mcp._prompt_manager.get_prompt("mounted_prompt")
assert prompt.enabled is False
def test_enable_nonexistent_tool(self, client):
"""Test enabling a non-existent tool returns 404."""
response = client.post("/tools/nonexistent_tool/enable")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.text == "Unknown tool: nonexistent_tool"
def test_disable_nonexistent_tool(self, client):
"""Test disabling a non-existent tool returns 404."""
response = client.post("/tools/nonexistent_tool/disable")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.text == "Unknown tool: nonexistent_tool"
def test_enable_nonexistent_resource(self, client):
"""Test enabling a non-existent resource returns 404."""
response = client.post("/resources/nonexistent://resource/enable")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.text == "Unknown resource: nonexistent://resource"
def test_disable_nonexistent_resource(self, client):
"""Test disabling a non-existent resource returns 404."""
response = client.post("/resources/nonexistent://resource/disable")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.text == "Unknown resource: nonexistent://resource"
def test_enable_nonexistent_prompt(self, client):
"""Test enabling a non-existent prompt returns 404."""
response = client.post("/prompts/nonexistent_prompt/enable")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.text == "Unknown prompt: nonexistent_prompt"
def test_disable_nonexistent_prompt(self, client):
"""Test disabling a non-existent prompt returns 404."""
response = client.post("/prompts/nonexistent_prompt/disable")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.text == "Unknown prompt: nonexistent_prompt"
class TestAuthComponentManagementRoutes:
"""Test the component management routes with authentication for tools, resources, and prompts."""
def setup_method(self):
"""Set up test fixtures."""
# Generate a key pair and create an auth provider
key_pair = RSAKeyPair.generate()
self.auth = JWTVerifier(
public_key=key_pair.public_key,
issuer="https://dev.example.com",
audience="my-dev-server",
)
self.mcp = FastMCP("TestServerWithAuth", auth=self.auth)
set_up_component_manager(
server=self.mcp, required_scopes=["tool:write", "tool:read"]
)
self.token = key_pair.create_token(
subject="dev-user",
issuer="https://dev.example.com",
audience="my-dev-server",
scopes=["tool:write", "tool:read"],
)
self.token_without_scopes = key_pair.create_token(
subject="dev-user",
issuer="https://dev.example.com",
audience="my-dev-server",
scopes=["tool:read"],
)
# Add test components
@self.mcp.tool
def test_tool() -> str:
"""Test tool for auth testing."""
return "test_tool_result"
@self.mcp.resource("data://test_resource")
def test_resource() -> str:
"""Test resource for auth testing."""
return "test_resource_result"
@self.mcp.prompt
def test_prompt() -> str:
"""Test prompt for auth testing."""
return "test_prompt_result"
# Create test client
self.client = TestClient(self.mcp.http_app())
async def test_unauthorized_enable_tool(self):
"""Test that unauthenticated requests to enable a tool are rejected."""
tool = await self.mcp._tool_manager.get_tool("test_tool")
tool.enabled = False
response = self.client.post("/tools/test_tool/enable")
assert response.status_code == 401
assert tool.enabled is False
async def test_authorized_enable_tool(self):
"""Test that authenticated requests to enable a tool are allowed."""
tool = await self.mcp._tool_manager.get_tool("test_tool")
tool.enabled = False
response = self.client.post(
"/tools/test_tool/enable", headers={"Authorization": "Bearer " + self.token}
)
assert response.status_code == 200
assert response.json() == {"message": "Enabled tool: test_tool"}
assert tool.enabled is True
async def test_unauthorized_disable_tool(self):
"""Test that unauthenticated requests to disable a tool are rejected."""
tool = await self.mcp._tool_manager.get_tool("test_tool")
tool.enabled = True
response = self.client.post("/tools/test_tool/disable")
assert response.status_code == 401
assert tool.enabled is True
async def test_authorized_disable_tool(self):
"""Test that authenticated requests to disable a tool are allowed."""
tool = await self.mcp._tool_manager.get_tool("test_tool")
tool.enabled = True
response = self.client.post(
"/tools/test_tool/disable",
headers={"Authorization": "Bearer " + self.token},
)
assert response.status_code == 200
assert response.json() == {"message": "Disabled tool: test_tool"}
assert tool.enabled is False
async def test_forbidden_enable_tool(self):
"""Test that requests with insufficient scopes are rejected."""
tool = await self.mcp._tool_manager.get_tool("test_tool")
tool.enabled = False
response = self.client.post(
"/tools/test_tool/enable",
headers={"Authorization": "Bearer " + self.token_without_scopes},
)
assert response.status_code == 403
assert tool.enabled is False
async def test_authorized_enable_resource(self):
"""Test that authenticated requests to enable a resource are allowed."""
resource = await self.mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = False
response = self.client.post(
"/resources/data://test_resource/enable",
headers={"Authorization": "Bearer " + self.token},
)
assert response.status_code == 200
assert response.json() == {"message": "Enabled resource: data://test_resource"}
assert resource.enabled is True
async def test_unauthorized_disable_resource(self):
"""Test that unauthenticated requests to disable a resource are rejected."""
resource = await self.mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = True
response = self.client.post("/resources/data://test_resource/disable")
assert response.status_code == 401
assert resource.enabled is True
async def test_forbidden_enable_resource(self):
"""Test that requests with insufficient scopes are rejected."""
resource = await self.mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = False
response = self.client.post(
"/resources/data://test_resource/disable",
headers={"Authorization": "Bearer " + self.token_without_scopes},
)
assert response.status_code == 403
assert resource.enabled is False
async def test_authorized_disable_resource(self):
"""Test that authenticated requests to disable a resource are allowed."""
resource = await self.mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = True
response = self.client.post(
"/resources/data://test_resource/disable",
headers={"Authorization": "Bearer " + self.token},
)
assert response.status_code == 200
assert response.json() == {"message": "Disabled resource: data://test_resource"}
assert resource.enabled is False
async def test_unauthorized_enable_prompt(self):
"""Test that unauthenticated requests to enable a prompt are rejected."""
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = False
response = self.client.post("/prompts/test_prompt/enable")
assert response.status_code == 401
assert prompt.enabled is False
async def test_authorized_enable_prompt(self):
"""Test that authenticated requests to enable a prompt are allowed."""
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = False
response = self.client.post(
"/prompts/test_prompt/enable",
headers={"Authorization": "Bearer " + self.token},
)
assert response.status_code == 200
assert response.json() == {"message": "Enabled prompt: test_prompt"}
assert prompt.enabled is True
async def test_unauthorized_disable_prompt(self):
"""Test that unauthenticated requests to disable a prompt are rejected."""
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = True
response = self.client.post("/prompts/test_prompt/disable")
assert response.status_code == 401
assert prompt.enabled is True
async def test_forbidden_disable_prompt(self):
"""Test that requests with insufficient scopes are rejected."""
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = True
response = self.client.post(
"/prompts/test_prompt/disable",
headers={"Authorization": "Bearer " + self.token_without_scopes},
)
assert response.status_code == 403
assert prompt.enabled is True
async def test_authorized_disable_prompt(self):
"""Test that authenticated requests to disable a prompt are allowed."""
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = True
response = self.client.post(
"/prompts/test_prompt/disable",
headers={"Authorization": "Bearer " + self.token},
)
assert response.status_code == 200
assert response.json() == {"message": "Disabled prompt: test_prompt"}
assert prompt.enabled is False
class TestComponentManagerWithPath:
"""Test component manager routes when mounted at a custom path."""
@pytest.fixture
def mcp_with_path(self):
mcp = FastMCP("TestServerWithPath")
set_up_component_manager(server=mcp, path="/test")
@mcp.tool
def test_tool() -> str:
return "test_tool_result"
@mcp.resource("data://test_resource")
def test_resource() -> str:
return "test_resource_result"
@mcp.prompt
def test_prompt() -> str:
return "test_prompt_result"
return mcp
@pytest.fixture
def client_with_path(self, mcp_with_path):
return TestClient(mcp_with_path.http_app())
async def test_enable_tool_route_with_path(self, client_with_path, mcp_with_path):
tool = await mcp_with_path._tool_manager.get_tool("test_tool")
tool.enabled = False
response = client_with_path.post("/test/tools/test_tool/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Enabled tool: test_tool"}
tool = await mcp_with_path._tool_manager.get_tool("test_tool")
assert tool.enabled is True
async def test_disable_resource_route_with_path(
self, client_with_path, mcp_with_path
):
resource = await mcp_with_path._resource_manager.get_resource(
"data://test_resource"
)
resource.enabled = True
response = client_with_path.post("/test/resources/data://test_resource/disable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Disabled resource: data://test_resource"}
resource = await mcp_with_path._resource_manager.get_resource(
"data://test_resource"
)
assert resource.enabled is False
async def test_enable_prompt_route_with_path(self, client_with_path, mcp_with_path):
prompt = await mcp_with_path._prompt_manager.get_prompt("test_prompt")
prompt.enabled = False
response = client_with_path.post("/test/prompts/test_prompt/enable")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"message": "Enabled prompt: test_prompt"}
prompt = await mcp_with_path._prompt_manager.get_prompt("test_prompt")
assert prompt.enabled is True
class TestComponentManagerWithPathAuth:
"""Test component manager routes with auth when mounted at a custom path."""
def setup_method(self):
# Generate a key pair and create an auth provider
key_pair = RSAKeyPair.generate()
self.auth = JWTVerifier(
public_key=key_pair.public_key,
issuer="https://dev.example.com",
audience="my-dev-server",
)
self.mcp = FastMCP("TestServerWithPathAuth", auth=self.auth)
set_up_component_manager(
server=self.mcp, path="/test", required_scopes=["tool:write", "tool:read"]
)
self.token = key_pair.create_token(
subject="dev-user",
issuer="https://dev.example.com",
audience="my-dev-server",
scopes=["tool:read", "tool:write"],
)
self.token_without_scopes = key_pair.create_token(
subject="dev-user",
issuer="https://dev.example.com",
audience="my-dev-server",
scopes=[],
)
@self.mcp.tool
def test_tool() -> str:
return "test_tool_result"
@self.mcp.resource("data://test_resource")
def test_resource() -> str:
return "test_resource_result"
@self.mcp.prompt
def test_prompt() -> str:
return "test_prompt_result"
self.client = TestClient(self.mcp.http_app())
async def test_unauthorized_enable_tool(self):
tool = await self.mcp._tool_manager.get_tool("test_tool")
tool.enabled = False
response = self.client.post("/test/tools/test_tool/enable")
assert response.status_code == 401
assert tool.enabled is False
async def test_forbidden_enable_tool(self):
tool = await self.mcp._tool_manager.get_tool("test_tool")
tool.enabled = False
response = self.client.post(
"/test/tools/test_tool/enable",
headers={"Authorization": "Bearer " + self.token_without_scopes},
)
assert response.status_code == 403
assert tool.enabled is False
async def test_authorized_enable_tool(self):
tool = await self.mcp._tool_manager.get_tool("test_tool")
tool.enabled = False
response = self.client.post(
"/test/tools/test_tool/enable",
headers={"Authorization": "Bearer " + self.token},
)
assert response.status_code == 200
assert response.json() == {"message": "Enabled tool: test_tool"}
tool = await self.mcp._tool_manager.get_tool("test_tool")
assert tool.enabled is True
async def test_unauthorized_disable_resource(self):
resource = await self.mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = True
response = self.client.post("/test/resources/data://test_resource/disable")
assert response.status_code == 401
assert resource.enabled is True
async def test_forbidden_disable_resource(self):
resource = await self.mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = True
response = self.client.post(
"/test/resources/data://test_resource/disable",
headers={"Authorization": "Bearer " + self.token_without_scopes},
)
assert response.status_code == 403
assert resource.enabled is True
async def test_authorized_disable_resource(self):
resource = await self.mcp._resource_manager.get_resource("data://test_resource")
resource.enabled = True
response = self.client.post(
"/test/resources/data://test_resource/disable",
headers={"Authorization": "Bearer " + self.token},
)
assert response.status_code == 200
assert response.json() == {"message": "Disabled resource: data://test_resource"}
resource = await self.mcp._resource_manager.get_resource("data://test_resource")
assert resource.enabled is False
async def test_unauthorized_enable_prompt(self):
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = False
response = self.client.post("/test/prompts/test_prompt/enable")
assert response.status_code == 401
assert prompt.enabled is False
async def test_forbidden_enable_prompt(self):
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = False
response = self.client.post(
"/test/prompts/test_prompt/enable",
headers={"Authorization": "Bearer " + self.token_without_scopes},
)
assert response.status_code == 403
assert prompt.enabled is False
async def test_authorized_enable_prompt(self):
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
prompt.enabled = False
response = self.client.post(
"/test/prompts/test_prompt/enable",
headers={"Authorization": "Bearer " + self.token},
)
assert response.status_code == 200
assert response.json() == {"message": "Enabled prompt: test_prompt"}
prompt = await self.mcp._prompt_manager.get_prompt("test_prompt")
assert prompt.enabled is True
|