refactor: McpError renamed to MCPError and flatten parameters (#1956)
Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com>
This commit is contained in:
parent
65c614e48e
commit
acba5478a9
6 changed files with 253 additions and 326 deletions
|
|
@ -11,7 +11,7 @@ from mcp.server import Server
|
|||
from mcp.server.lowlevel import NotificationOptions
|
||||
from mcp.server.models import InitializationOptions
|
||||
from mcp.server.session import ServerSession
|
||||
from mcp.shared.exceptions import McpError
|
||||
from mcp.shared.exceptions import MCPError
|
||||
from mcp.shared.message import ServerMessageMetadata, SessionMessage
|
||||
from mcp.shared.response_router import ResponseRouter
|
||||
from mcp.shared.session import RequestResponder
|
||||
|
|
@ -506,7 +506,7 @@ async def test_default_task_handlers_via_enable_tasks() -> None:
|
|||
assert get_result.status == "working"
|
||||
|
||||
# Test get_task (default handler - not found path)
|
||||
with pytest.raises(McpError, match="not found"):
|
||||
with pytest.raises(MCPError, match="not found"):
|
||||
await client_session.send_request(
|
||||
GetTaskRequest(params=GetTaskRequestParams(task_id="nonexistent-task")),
|
||||
GetTaskResult,
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import pytest
|
|||
|
||||
from mcp.server.experimental.task_context import ServerTaskContext
|
||||
from mcp.server.experimental.task_result_handler import TaskResultHandler
|
||||
from mcp.shared.exceptions import McpError
|
||||
from mcp.shared.exceptions import MCPError
|
||||
from mcp.shared.experimental.tasks.in_memory_task_store import InMemoryTaskStore
|
||||
from mcp.shared.experimental.tasks.message_queue import InMemoryTaskMessageQueue
|
||||
from mcp.types import (
|
||||
|
|
@ -164,7 +164,7 @@ async def test_server_task_context_fail_with_notify() -> None:
|
|||
|
||||
@pytest.mark.anyio
|
||||
async def test_elicit_raises_when_client_lacks_capability() -> None:
|
||||
"""Test that elicit() raises McpError when client doesn't support elicitation."""
|
||||
"""Test that elicit() raises MCPError when client doesn't support elicitation."""
|
||||
store = InMemoryTaskStore()
|
||||
mock_session = Mock()
|
||||
mock_session.check_client_capability = Mock(return_value=False)
|
||||
|
|
@ -180,7 +180,7 @@ async def test_elicit_raises_when_client_lacks_capability() -> None:
|
|||
handler=handler,
|
||||
)
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
await ctx.elicit(message="Test?", requested_schema={"type": "object"})
|
||||
|
||||
assert "elicitation capability" in exc_info.value.error.message
|
||||
|
|
@ -190,7 +190,7 @@ async def test_elicit_raises_when_client_lacks_capability() -> None:
|
|||
|
||||
@pytest.mark.anyio
|
||||
async def test_create_message_raises_when_client_lacks_capability() -> None:
|
||||
"""Test that create_message() raises McpError when client doesn't support sampling."""
|
||||
"""Test that create_message() raises MCPError when client doesn't support sampling."""
|
||||
store = InMemoryTaskStore()
|
||||
mock_session = Mock()
|
||||
mock_session.check_client_capability = Mock(return_value=False)
|
||||
|
|
@ -206,7 +206,7 @@ async def test_create_message_raises_when_client_lacks_capability() -> None:
|
|||
handler=handler,
|
||||
)
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
await ctx.create_message(messages=[], max_tokens=100)
|
||||
|
||||
assert "sampling capability" in exc_info.value.error.message
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from datetime import datetime, timedelta, timezone
|
|||
|
||||
import pytest
|
||||
|
||||
from mcp.shared.exceptions import McpError
|
||||
from mcp.shared.exceptions import MCPError
|
||||
from mcp.shared.experimental.tasks.helpers import cancel_task
|
||||
from mcp.shared.experimental.tasks.in_memory_task_store import InMemoryTaskStore
|
||||
from mcp.types import INVALID_PARAMS, CallToolResult, TaskMetadata, TextContent
|
||||
|
|
@ -347,8 +347,8 @@ async def test_cancel_task_succeeds_for_working_task(store: InMemoryTaskStore) -
|
|||
|
||||
@pytest.mark.anyio
|
||||
async def test_cancel_task_rejects_nonexistent_task(store: InMemoryTaskStore) -> None:
|
||||
"""Test cancel_task raises McpError with INVALID_PARAMS for nonexistent task."""
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
"""Test cancel_task raises MCPError with INVALID_PARAMS for nonexistent task."""
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
await cancel_task(store, "nonexistent-task-id")
|
||||
|
||||
assert exc_info.value.error.code == INVALID_PARAMS
|
||||
|
|
@ -357,11 +357,11 @@ async def test_cancel_task_rejects_nonexistent_task(store: InMemoryTaskStore) ->
|
|||
|
||||
@pytest.mark.anyio
|
||||
async def test_cancel_task_rejects_completed_task(store: InMemoryTaskStore) -> None:
|
||||
"""Test cancel_task raises McpError with INVALID_PARAMS for completed task."""
|
||||
"""Test cancel_task raises MCPError with INVALID_PARAMS for completed task."""
|
||||
task = await store.create_task(metadata=TaskMetadata(ttl=60000))
|
||||
await store.update_task(task.task_id, status="completed")
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
await cancel_task(store, task.task_id)
|
||||
|
||||
assert exc_info.value.error.code == INVALID_PARAMS
|
||||
|
|
@ -370,11 +370,11 @@ async def test_cancel_task_rejects_completed_task(store: InMemoryTaskStore) -> N
|
|||
|
||||
@pytest.mark.anyio
|
||||
async def test_cancel_task_rejects_failed_task(store: InMemoryTaskStore) -> None:
|
||||
"""Test cancel_task raises McpError with INVALID_PARAMS for failed task."""
|
||||
"""Test cancel_task raises MCPError with INVALID_PARAMS for failed task."""
|
||||
task = await store.create_task(metadata=TaskMetadata(ttl=60000))
|
||||
await store.update_task(task.task_id, status="failed")
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
await cancel_task(store, task.task_id)
|
||||
|
||||
assert exc_info.value.error.code == INVALID_PARAMS
|
||||
|
|
@ -383,11 +383,11 @@ async def test_cancel_task_rejects_failed_task(store: InMemoryTaskStore) -> None
|
|||
|
||||
@pytest.mark.anyio
|
||||
async def test_cancel_task_rejects_already_cancelled_task(store: InMemoryTaskStore) -> None:
|
||||
"""Test cancel_task raises McpError with INVALID_PARAMS for already cancelled task."""
|
||||
"""Test cancel_task raises MCPError with INVALID_PARAMS for already cancelled task."""
|
||||
task = await store.create_task(metadata=TaskMetadata(ttl=60000))
|
||||
await store.update_task(task.task_id, status="cancelled")
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
await cancel_task(store, task.task_id)
|
||||
|
||||
assert exc_info.value.error.code == INVALID_PARAMS
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import anyio
|
|||
import pytest
|
||||
|
||||
from mcp.server.experimental.task_result_handler import TaskResultHandler
|
||||
from mcp.shared.exceptions import McpError
|
||||
from mcp.shared.exceptions import MCPError
|
||||
from mcp.shared.experimental.tasks.in_memory_task_store import InMemoryTaskStore
|
||||
from mcp.shared.experimental.tasks.message_queue import InMemoryTaskMessageQueue, QueuedMessage
|
||||
from mcp.shared.experimental.tasks.resolver import Resolver
|
||||
|
|
@ -71,11 +71,11 @@ async def test_handle_returns_result_for_completed_task(
|
|||
async def test_handle_raises_for_nonexistent_task(
|
||||
store: InMemoryTaskStore, queue: InMemoryTaskMessageQueue, handler: TaskResultHandler
|
||||
) -> None:
|
||||
"""Test that handle() raises McpError for nonexistent task."""
|
||||
"""Test that handle() raises MCPError for nonexistent task."""
|
||||
mock_session = Mock()
|
||||
request = GetTaskPayloadRequest(params=GetTaskPayloadRequestParams(task_id="nonexistent"))
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
await handler.handle(request, mock_session, "req-1")
|
||||
|
||||
assert "not found" in exc_info.value.error.message
|
||||
|
|
@ -214,7 +214,7 @@ async def test_route_error_resolves_pending_request_with_exception(
|
|||
assert result is True
|
||||
assert resolver.done()
|
||||
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
await resolver.wait()
|
||||
assert exc_info.value.error.message == "Something went wrong"
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import pytest
|
||||
|
||||
from mcp.shared.exceptions import McpError
|
||||
from mcp import MCPError
|
||||
from mcp.shared.experimental.tasks.capabilities import (
|
||||
check_tasks_capability,
|
||||
has_task_augmented_elicitation,
|
||||
|
|
@ -231,15 +231,15 @@ class TestRequireTaskAugmentedElicitation:
|
|||
"""Tests for require_task_augmented_elicitation function."""
|
||||
|
||||
def test_raises_when_none(self) -> None:
|
||||
"""Raises McpError when client_caps is None."""
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
"""Raises MCPError when client_caps is None."""
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
require_task_augmented_elicitation(None)
|
||||
assert "task-augmented elicitation" in str(exc_info.value)
|
||||
|
||||
def test_raises_when_missing(self) -> None:
|
||||
"""Raises McpError when capability is missing."""
|
||||
"""Raises MCPError when capability is missing."""
|
||||
caps = ClientCapabilities()
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
require_task_augmented_elicitation(caps)
|
||||
assert "task-augmented elicitation" in str(exc_info.value)
|
||||
|
||||
|
|
@ -259,15 +259,15 @@ class TestRequireTaskAugmentedSampling:
|
|||
"""Tests for require_task_augmented_sampling function."""
|
||||
|
||||
def test_raises_when_none(self) -> None:
|
||||
"""Raises McpError when client_caps is None."""
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
"""Raises MCPError when client_caps is None."""
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
require_task_augmented_sampling(None)
|
||||
assert "task-augmented sampling" in str(exc_info.value)
|
||||
|
||||
def test_raises_when_missing(self) -> None:
|
||||
"""Raises McpError when capability is missing."""
|
||||
"""Raises MCPError when capability is missing."""
|
||||
caps = ClientCapabilities()
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
require_task_augmented_sampling(caps)
|
||||
assert "task-augmented sampling" in str(exc_info.value)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
import pytest
|
||||
|
||||
from mcp.server.experimental.request_context import Experimental
|
||||
from mcp.shared.exceptions import McpError
|
||||
from mcp.shared.exceptions import MCPError
|
||||
from mcp.types import (
|
||||
METHOD_NOT_FOUND,
|
||||
TASK_FORBIDDEN,
|
||||
|
|
@ -58,7 +58,7 @@ def test_validate_task_mode_required_without_task_returns_error() -> None:
|
|||
|
||||
def test_validate_task_mode_required_without_task_raises_by_default() -> None:
|
||||
exp = Experimental(task_metadata=None)
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
exp.validate_task_mode(TASK_REQUIRED)
|
||||
assert exc_info.value.error.code == METHOD_NOT_FOUND
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ def test_validate_task_mode_forbidden_with_task_returns_error() -> None:
|
|||
|
||||
def test_validate_task_mode_forbidden_with_task_raises_by_default() -> None:
|
||||
exp = Experimental(task_metadata=TaskMetadata(ttl=60000))
|
||||
with pytest.raises(McpError) as exc_info:
|
||||
with pytest.raises(MCPError) as exc_info:
|
||||
exp.validate_task_mode(TASK_FORBIDDEN)
|
||||
assert exc_info.value.error.code == METHOD_NOT_FOUND
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue