Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

999 B
Raw Blame

Persistent Session State

This example demonstrates session-scoped state that persists across tool calls within the same MCP session.

What it shows

  • State set in one tool call is readable in subsequent calls
  • Different clients have isolated state (same keys, different values)
  • Reconnecting creates a new session with fresh state

Running

HTTP transport:

# Terminal 1: Start the server
uv run python server.py

# Terminal 2: Run the client
uv run python client.py

STDIO transport (in-process):

uv run python client_stdio.py

Example output

Each line below is a separate tool call

Alice connects
  session a9f6eaa3
  set user = Alice
  set secret = alice-password
  get user β†’ Alice
  get secret β†’ alice-password

Bob connects (different session)
  session 0c3bffc5
  get user β†’ not found
  get secret β†’ not found
  set user = Bob
  get user β†’ Bob

Alice reconnects (new session)
  session e39640e3
  get user β†’ not found