Neural Mastery
← Practice
MCP Session Lifecycle State Transitionmedium

MCP Session Lifecycle State Transition

medium Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement the real MCP session lifecycle state machine, governing valid connection state transitions between a client and server.

Task

Implement session_transition(current_state, event) with these valid transitions: ('uninitialized','initialize')->'initializing'; ('initializing','initialized')->'ready'; ('ready','shutdown')->'shutting_down'; ('shutting_down','exit')->'closed'; from ANY state, event 'error' transitions to 'closed'. Any other (state, event) pair is invalid -- return current_state unchanged.

Function Signature

session_transition(current_state: str, event: str) -> str

Examples

Example 1: normal init sequence step 1
Input: current_state = 'uninitialized' event = 'initialize'
Output: 'initializing'
Example 2: normal init sequence step 2
Input: current_state = 'initializing' event = 'initialized'
Output: 'ready'
Example 3: error from any state closes
Input: current_state = 'ready' event = 'error'
Output: 'closed'

Constraints

  • current_state and event are strings
Python3Saved ✓
def session_transition(current_state, event):
# Your implementation here
pass

Case 1: normal init sequence step 1
Input: current_state = 'uninitialized' event = 'initialize'
Expected: 'initializing'
Case 2: normal init sequence step 2
Input: current_state = 'initializing' event = 'initialized'
Expected: 'ready'
Case 3: error from any state closes
Input: current_state = 'ready' event = 'error'
Expected: 'closed'