Skip to main content

Backend Functions Reference

The get_nbm and get_ads functions are essential utility functions in the scmcphub ecosystem that provide access to the backend managers during MCP server execution. These functions are used to retrieve the appropriate backend context based on the execution mode.

Overview

These functions serve as accessors to the backend managers that are injected into the MCP server’s lifespan context. They are crucial for:
  • Tool Mode: Accessing AdataManager for AnnData operations
  • Code Mode: Accessing NotebookManager for Jupyter notebook operations

Function Reference

get_ads()

Retrieves the AdataManager instance from the current request context.

Signature

Returns

  • Type: AdataManager
  • Description: The AdataManager instance that manages AnnData objects

Usage Context

  • Mode: Tool Mode (tool-mode)
  • Backend: AdataManager
  • Purpose: Managing AnnData objects for single-cell analysis

Example

get_nbm()

Retrieves the NotebookManager instance from the current request context.

Signature

Returns

  • Type: NotebookManager
  • Description: The NotebookManager instance that manages Jupyter notebooks

Usage Context

  • Mode: Code Mode (code-mode)
  • Backend: NotebookManager
  • Purpose: Managing Jupyter notebook execution

Example

Backend Managers

AdataManager

The AdataManager is responsible for managing AnnData objects in tool mode.

Key Features

  • Multi-type Support: Manages different types of AnnData (exp, activity, cnv, splicing)
  • Active Sample Management: Tracks the currently active sample ID
  • Sample ID Validation: Ensures data consistency across operations

Main Methods

Example Usage

NotebookManager

The NotebookManager is responsible for managing Jupyter notebooks in code mode.

Key Features

  • Multiple Notebook Support: Manages multiple notebook instances
  • Active Notebook Tracking: Maintains the currently active notebook
  • Kernel Management: Handles Jupyter kernel lifecycle

Main Methods

Example Usage

Context System

Both functions rely on FastMCP’s context system to access the backend managers.

Context Flow

  1. Server Initialization: Backend manager is created and injected into lifespan context
  2. Request Processing: Context is available during tool execution
  3. Function Call: get_context() retrieves the current request context
  4. Backend Access: Backend manager is extracted from lifespan context

Context Structure

Mode-Specific Usage

Tool Mode (Default)

Code Mode

Error Handling

Common Issues

  1. Context Not Available: Ensure the function is called within an MCP tool context
  2. Wrong Backend Type: Verify the correct backend is configured for your use case
  3. Missing Dependencies: Ensure FastMCP dependencies are properly installed

Error Examples

Best Practices

1. Import Location

Import these functions at the module level:

2. Error Checking

Always check if the backend is available:

3. Type Hints

Use proper type hints for better code clarity:

4. Context Validation

Validate the backend type when needed:

Integration Examples

Complete Tool Example

Complete Code Mode Example