mcp/server API¶
dnallm.mcp.server ¶
DNALLM MCP Server Implementation.
This module implements the main MCP (Model Context Protocol) server using the FastMCP framework with Server-Sent Events (SSE) support for real-time DNA sequence prediction.
The server provides a comprehensive set of tools for DNA sequence analysis, including: - Single sequence prediction with specific models - Batch processing of multiple sequences - Multi-model prediction and comparison - Real-time streaming predictions with progress updates - Model management and health monitoring
Architecture
The server is built on top of the FastMCP framework, which provides MCP protocol implementation with multiple transport options (stdio, SSE, HTTP). The server manages DNA language models through a ModelManager and handles configuration through a ConfigManager.
Transport Protocols
- stdio: Standard input/output for CLI tools
- streamable-http: HTTP-based streaming protocol (recommended for remote connections, per MCP spec 2025-11-25)
- sse: Server-Sent Events for real-time web applications (legacy, deprecated in MCP spec 2025-11-25 but retained for backward compatibility)
Example
Basic server initialization:
server = DNALLMMCPServer("config/server_config.yaml")
await server.initialize()
server.start_server(host="127.0.0.1", port=8000,
transport="streamable-http")
Note
This server requires proper configuration files and model setup before initialization. See the configuration documentation for details.
Classes¶
DNALLMMCPServer ¶
DNALLMMCPServer(config_path)
DNALLM MCP Server implementation using FastMCP framework with SSE support.
This class provides a comprehensive MCP server for DNA language model inference and analysis. It supports multiple transport protocols and provides real-time streaming capabilities for DNA sequence prediction tasks.
The server manages multiple DNA language models and provides various prediction modes including single sequence prediction, batch processing, and multi-model comparison. All operations support progress reporting through streaming transports for real-time user feedback.
Attributes:
| Name | Type | Description |
|---|---|---|
config_path |
str
|
Path to the main server configuration file |
config_manager |
MCPConfigManager
|
Handles configuration management |
model_manager |
ModelManager
|
Manages model loading and prediction |
app |
FastMCP | None
|
Main FastMCP application instance |
sse_app |
SSE application instance (unused, FastMCP handles SSE internally) |
|
_initialized |
bool
|
Server initialization status flag |
Example
Initialize and start the server:
# Create server instance
server = DNALLMMCPServer("config/mcp_server_config.yaml")
# Initialize asynchronously
await server.initialize()
# Start with Streamable HTTP transport (recommended)
server.start_server(host="0.0.0.0", port=8000,
transport="streamable-http")
# Start with SSE transport (legacy, backward compatible)
server.start_server(host="0.0.0.0", port=8000,
transport="sse")
Note
The server must be initialized before starting. Configuration files must contain valid model and server settings.
Initialize the MCP server instance.
Sets up the server with configuration and model managers, but does not load models or start the server. Call initialize() and start_server() separately for complete setup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Absolute or relative path to the main MCP server configuration file. This file should contain server settings, model configurations, and transport options. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the configuration file doesn't exist |
ConfigurationError
|
If the configuration file is invalid |
Example
server = DNALLMMCPServer("/path/to/config.yaml")
Note
The configuration directory and filename are extracted separately to support the MCPConfigManager's directory-based configuration loading strategy.
Source code in dnallm/mcp/server.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
Methods:¶
get_server_info ¶
get_server_info()
Get server information.
Source code in dnallm/mcp/server.py
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 | |
initialize
async
¶
initialize()
Initialize the server and load all enabled models.
This method performs the complete server initialization process: 1. Checks if already initialized (idempotent operation) 2. Loads and validates server configuration 3. Creates the FastMCP application instance 4. Registers all MCP tools 5. Loads all enabled DNA language models
The initialization is asynchronous because model loading can be time-consuming, especially for large transformer models.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If server configuration cannot be loaded |
ModelLoadError
|
If critical models fail to load |
ConfigurationError
|
If configuration is invalid |
Example
server = DNALLMMCPServer("config.yaml")
await server.initialize() # Required before starting
Note
This method is idempotent - calling it multiple times has no additional effect after the first successful initialization.
Source code in dnallm/mcp/server.py
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 | |
shutdown
async
¶
shutdown()
Shutdown the server and cleanup resources.
Source code in dnallm/mcp/server.py
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 | |
start_server ¶
start_server(
host="127.0.0.1", port=8000, transport="stdio"
)
Start the MCP server with the specified transport protocol.
This method starts the server using one of the supported transport protocols. The server must be initialized before calling this method. The transport protocol determines how the server communicates with clients:
- stdio: Standard input/output for CLI tools and automation
- streamable-http: HTTP-based streaming for REST API integration (recommended per MCP spec 2025-11-25)
- sse: Server-Sent Events (legacy, deprecated in MCP spec 2025-11-25 but retained for backward compatibility)
Session management for the streamable-http transport is handled
entirely by the FastMCP SDK via StreamableHTTPSessionManager.
Callers do not need to create, track, or clean up MCP-Session-Id
headers manually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Host address to bind the server to. Defaults to "127.0.0.1". Use "0.0.0.0" for all interfaces. |
'127.0.0.1'
|
port
|
int
|
Port number to bind the server to. Defaults to 8000. Only used for HTTP-based transports. |
8000
|
transport
|
str
|
Transport protocol to use. Choices: "stdio", "streamable-http", "sse". Defaults to "stdio". |
'stdio'
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If server is not initialized before starting |
OSError
|
If port is already in use or host is invalid |
ConfigurationError
|
If transport configuration is invalid |
Example
# Start with Streamable HTTP (recommended)
server.start_server(
host="0.0.0.0",
port=8000,
transport="streamable-http"
)
# Start with SSE (legacy, backward compatible)
server.start_server(
host="0.0.0.0",
port=8000,
transport="sse"
)
# Start with stdio for CLI tools
server.start_server(transport="stdio")
Note
This method is blocking and will run until the server is stopped. For SSE and HTTP transports, uvicorn handles graceful shutdown on SIGINT/SIGTERM signals.
Source code in dnallm/mcp/server.py
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 | |
Functions:¶
initialize_mcp_server
async
¶
initialize_mcp_server(config_path)
Initialize the MCP server asynchronously.
This is a convenience function that creates and initializes a DNALLMMCPServer instance. It's designed to be called from asyncio.run() or other async contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path to the server configuration file |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DNALLMMCPServer |
DNALLMMCPServer
|
Fully initialized server instance ready to start |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If configuration is invalid |
ModelLoadError
|
If model loading fails |
RuntimeError
|
If initialization fails |
Example
server = await initialize_mcp_server("config.yaml")
server.start_server(transport="sse")
Note
This function combines server creation and initialization for convenience in async entry points.
Source code in dnallm/mcp/server.py
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 | |
main ¶
main()
Main entry point for the DNALLM MCP server CLI.
This function provides a command-line interface for starting the DNALLM MCP server with various configuration options. It handles argument parsing, configuration validation, server initialization, and graceful error handling.
The CLI supports multiple transport protocols and comprehensive configuration options for production deployment. It includes proper error handling and logging for troubleshooting.
Command Line Arguments
--config: Path to server configuration file --host: Host address to bind to (default: 0.0.0.0) --port: Port number to bind to (default: 8000) --transport: Protocol (stdio/sse/streamable-http, default: stdio) --log-level: Logging verbosity (DEBUG/INFO/WARNING/ERROR/CRITICAL) --version: Display version information
Example Usage
# Start with SSE transport
python server.py --config config.yaml --transport sse --port 8000
# Start with stdio (default)
python server.py --config config.yaml
# Start with debug logging
python server.py --config config.yaml --log-level DEBUG
Exit Codes
0: Successful execution 1: Configuration file not found or server error
Note
The server runs in blocking mode. Use Ctrl+C to stop gracefully. For SSE/HTTP transports, uvicorn handles signal processing automatically.
Source code in dnallm/mcp/server.py
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 | |