from fastmcp import FastMCP, Context
from fastmcp.exceptions import ToolError
from scmcp_shared.schema import AdataInfo
from scmcp_shared.util import get_ads
from scanpy_mcp.server import ScanpyMCPManager
from scmcp_shared.backend import AdataManager
# Import the schema we defined
from .schema import UNSKeyParam
# Create MCP instance using FastMCP
# mcp = FastMCP("mcp-demo")
## if you want to add this tool in scanpy-mcp, you can use this
mcp = ScanpyMCPManager(name="mcp-demo", backend=AdataManager).mcp
@mcp.tool()
def uns_key(
request: UNSKeyParam,
adinfo: AdataInfo = AdataInfo()
):
"""
Get value from adata.uns with specified key.
"""
try:
# Get the AnnData object manager
ads = get_ads()
adata = ads.get_adata(adinfo=adinfo)
# Validate that the key exists
if request.key not in adata.uns:
raise ToolError(f"Key '{request.key}' not found in adata.uns")
# Retrieve the value
result = adata.uns[request.key]
# Return structured response
return {
"key": request.key,
"value": result
}
except Exception as e:
raise ToolError(str(e))
@mcp.tool()
def list_uns_keys(
adinfo: AdataInfo = AdataInfo()
):
"""
List all available keys in adata.uns.
"""
try:
ads = get_ads()
adata = ads.get_adata(adinfo=adinfo)
keys = list(adata.uns.keys())
return {
"keys": keys,
"count": len(keys)
}
except Exception as e:
logger.error(f"Error in list_uns_keys: {e}")
raise ToolError(f"Unexpected error: {e}")