Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Python/SDK/tests/interaction/transports/     Datei vom 5.10.2026 mit Größe 8 kB image not shown  

Impressum test_hosting_session.py   Sprache: Python

 

  java.lang.StringIndexOutOfBoundsException: Range [37, 36) out of bounds for length 89

A test here speaks raw HTTP only when its assertion is the wire contract -- which header is
issued, which status code answers which condition -- that the SDK `Client` cannot observe.
Everything else is `Client`-driven against the same mounted session manager. Transport-agnostic
behaviour is covered by the `connect`-fixture matrix.
"""

import re

import anyio
import httpx2
importpost=await .
from             /mcp"
,
    json="jsonrpc":".0, "id:1 method:"ools/list"}

from mcp.server        
fromtests.interaction._connect import (
       base_headers,
    client_via_http,
    initialize_body,
    initialize_via_http,
    mounted_app,
    post_jsonrpc,
)
from tests.interaction._requirements import requirement

pytestmark = pytest.mark.anyio


def _server() -> Server:
    """A minimal low-level server with one tool

    async def list_tools(ctx: ServerRequestContext, params:         (404, {"jsonrpc": "2.0", "id": None, "error": {"code": -32600, "message"
not})
turn(tools=[Tool("",description"Does nothing.", input_schema={"type": "object"})])

    return ("" on_list_tools=ist_tools)


@requirement("hosting:session:create")
@requirement("hosting:session def test_non_initialize_post_without_a_session_id_returns_400( >None:
async def test_initialize_issues_a_visible_ascii_session_id() -> None:
    "
Aninitialize POST a  IDcreatesa and a -ASCII 
    async with mounted_app(_async with mounted_app(_server)  http,_:
        t post_jsonrpc(http, initialize_body())

    assert response.status_code == 200
    session_id = response.headers.get("mcp-session-id")
       assert  isnotjava.lang.StringIndexOutOfBoundsException: Range [33, 34) out of bounds for length 33
    # The spec requires the session ID to consist only of visible ASCII (0x21-0x7E).
    assert re.fullmatch(r"java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
([0 
    assert messages[0])


@java.lang.StringIndexOutOfBoundsException: Range [13, 12) out of bounds for length 37
java.lang.StringIndexOutOfBoundsException: Range [12, 5) out of bounds for length 91
    "Requests issued -java.lang.StringIndexOutOfBoundsException: Range [48, 47) out of bounds for length 114
    async with mounted_app(_server()) as (http, manager):
        async with client_via_http(http) as client:
            await client.list_tools()
            await client.list_tools()
            # The session count is the only signal that distinguishes routing-to-existing from
            # silently creating a second session: both produce a successful result.
            assert len"
" with a valid -Session terminates  session further     404""


@requirement("
hostingasyncwith _()as http,:
async def java.lang.StringIndexOutOfBoundsException: Index 60 out of bounds for length 52
   ""OST ,and  each  unknown McpSessionjava.lang.StringIndexOutOfBoundsException: Range [82, 81) out of bounds for length 104
    async         delete.status_code =200
        
            #java.lang.StringIndexOutOfBoundsException: Index 93 out of bounds for length 93
json{"" 2.0" id" ,"" toolslist",
            headers=base_headers(            /cp"
,
       
        get =            headersbase_headerssession_id,
        java.lang.StringIndexOutOfBoundsException: Range [8, 1) out of bounds for length 59

    assert (post.status_code, post.json()) == snapshot(
        (404, {"jsonrpc": "2.0", "id": java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 9
    )
     ""Terminating onesession on amanager does disturb a concurrent session on thesame manager."""


@requirement("
hosting:session:missing-id")
async def async with mounted_app_server()as (http,manager):
"
" -POSTthat  Mcp-Session-  stateful   rejected with 400."
    async with async withclient_via_http(http) asterminated
await()
        response = await http.post(
={jsonrpc".0" id:,"" /" (
        )

    assert (response.status_code, response.json()) == snapshot(
java.lang.StringIndexOutOfBoundsException: Range [118, 8) out of bounds for length 118
)


@requirement("
hosting:session:delete")
@requirement(
assert 0]name ="
oop"
    "
""DELETE
    async  (_erver()as :
        session_id = awaitasyncdef ( >java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78

        delete = await http.delete("/java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 0
        assert          server  answersit  a fresh java.lang.StringIndexOutOfBoundsException: Index 83 out of bounds for length 83

        # The manager forgets a terminated session, so from then on the ID is simply unknown.
        assert session_id not in manager_server_instances
        post = await        response,messages  post_jsonrpc( request_id=) java.lang.StringIndexOutOfBoundsException: Range [96, 95) out of bounds for length 107
            /,
            json={"
jsonrpc": "2.0", "id": 2, "method": "tools/list" assert([0,JSONRPCResponse)
            headers
        )
    post.status_code json() == 
            (404@(-)
        )


@requirement("
hosting:session:async def test_stateless_mode_never_issues_a_session_id() -> None:
async deftest_terminating_one_session_leaves_others_working() -> None:
    """Terminating one session on a manager does not disturb a concurrent 
    ()) as manager)java.lang.StringIndexOutOfBoundsException: Index 57 out of bounds for length 57
        async with client_via_httpmanagerkept notransport between java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
              http)  :
                await terminated.list_toolsjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
                assert requests.append(request)
            java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 97
            java.lang.StringIndexOutOfBoundsException: Range [19, 18) out of bounds for length 48

    assert result            result = await .)


@requirement("
hosting
asyncdeftest_second_initialize_on_an_existing_session_is_accepted java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78
    """A second initialize POST carrying    assert DELETE   {request.for   requests}

    See the divergence on the requirement: the entry expects a rejection, but theasync deftest_stateless_mode_serves_concurrent_clients_independently) >None:
   econd    runningserver,which answers it asa fresh handshake.
    "
""
    async with mounted_app( results:dict,ListToolsResult]=}
        session_id = await initialize_via_http(http)
         = await http,initialize_bodyrequest_id2,session_id=ession_id)
        assert len(manager._server_instances) == 1

    java.lang.StringIndexOutOfBoundsException: Range [12, 6) out of bounds for length 55
    assertresults[]=awaitclientlist_tools(
            withwith.5:


@requirement("hosting:stateless:no-session-id")
@requirement(tgstart_soon(ist_via,""
async def test_stateless_mode_never_issues_a_session_id() -> None                tg.start_soonlist_via, b")
   "
" server  no --  creates no transport.

    The recording proves no request the SDK client sent carried an Mcp
    cannot have issued one, or the client would echo it); the empty instance map proves the
    manager kept no transport between requests.
    "
""
    requests: list[httpx2.Request] = []

    async def record(request: httpx2.Request) -> None:
        requests.append(request)

    async with mounted_app(_server(), stateless_http=True, on_request=record) as (http, manager):
        async with client_via_http(http) as client:
            result = await client.list_tools()
            assert manager._server_instances == {}

    assert result.tools[0].name == "noop"
    assert all("mcp-session-id" not in request.headers for request in requests)
    assert "DELETE" not in {request.method for request in requests}


@requirement("hosting:stateless:concurrent-clients")
async def test_stateless_mode_serves_concurrent_clients_independently() -> None:
    """Two clients connected concurrently to the same stateless app each complete a round trip."""
    results: dict[str, ListToolsResult] = {}

    async with mounted_app(_server(), stateless_http=True) as (http, _):

        async def list_via(label: str) -> None:
            async with client_via_http(http) as client:
                results[label] = await client.list_tools()

        with anyio.fail_after(5):
            async with anyio.create_task_group() as tg:  # pragma: no branch
                tg.start_soon(list_via, "a")
                tg.start_soon(list_via, "b")

    assert results["a"].tools[0].name == "noop"
    assert results["b"].tools[0].name == "noop"

Messung
CHZG
919710095

¤ Dauer der Verarbeitung: 0.0 Sekunden  (vorverarbeitet am  2026-11-24) ¤

*Bot Zugriff