State¶
Server state management for the idfkit MCP server.
State is keyed per MCP session so that concurrent streamable-http
connections each get their own model, simulation result, etc. For
stdio transport (single client) a fixed "stdio" session ID is
used, behaving identically to the previous singleton approach.
MAX_CHANGE_LOG = 100
module-attribute
¶
Maximum change-log entries retained per session.
ServerState
dataclass
¶
Holds the active document, schema, and simulation result for one session.
Instances are created and managed by :func:get_state, which keys them
by MCP session ID. For stdio transport a fixed "stdio" key is
used (single-client, equivalent to the old singleton). For
streamable-http each connection gets an isolated instance keyed by
the Mcp-Session-Id header.
Session state (file_path, simulation run dir, weather file) is persisted to a JSON file on disk so that clients that restart the server between turns (e.g. Codex) can transparently resume where they left off. Persistence is disabled for HTTP sessions since they receive a new session ID on every connection.
Source code in src/idfkit_mcp/state.py
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 | |
clear_session()
¶
Delete the session file and reset model/simulation state.
Uploads are preserved so the user can re-load without re-uploading.
Source code in src/idfkit_mcp/state.py
get_or_load_docs_index(version=None)
¶
Return cached docs index, loading on first use or version change.
Resolution order:
1. IDFKIT_DOCS_DIR env var (local dist/ directory)
2. Local cache (~/.cache/idfkit/docs/)
3. Download from docs.idfkit.com and cache locally
Returns:
| Type | Description |
|---|---|
tuple[list[dict[str, object]], str, str]
|
Tuple of (items list, separator regex string, resolved version string). |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the index cannot be loaded from any source. |
Source code in src/idfkit_mcp/state.py
get_or_load_schema(version=None)
¶
Return the active schema, or load one for the given version.
Source code in src/idfkit_mcp/state.py
get_or_load_station_index()
¶
Return the cached station index, loading it on first use.
Source code in src/idfkit_mcp/state.py
record_change(tool_name, summary=None)
¶
Append a mutation entry to the in-memory change log, capped at MAX_CHANGE_LOG.
Source code in src/idfkit_mcp/state.py
require_model()
¶
Return the active document, auto-restoring from session if needed.
Source code in src/idfkit_mcp/state.py
require_schema()
¶
Return the active schema, auto-restoring from session if needed.
Source code in src/idfkit_mcp/state.py
require_simulation_result()
¶
Return the simulation result, auto-restoring from session if needed.
Source code in src/idfkit_mcp/state.py
save_session()
¶
Persist restorable state paths to the session file.
Source code in src/idfkit_mcp/state.py
current_session_id()
¶
get_state()
¶
Return the ServerState for the current MCP session.
Creates a new state on first access for a given session ID and evicts the least-recently-used session when the registry is full.
Source code in src/idfkit_mcp/state.py
reset_sessions()
¶
Clear all sessions and reset to the default "stdio" session.
Intended for test fixtures only.
session_scope_from_context(ctx)
¶
Temporarily bind the current session ID from a FastMCP context.
Source code in src/idfkit_mcp/state.py
session_uploads_dir(session_id)
¶
Return the per-session directory for uploaded files materialized to disk.