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
- sse: Server-Sent Events for real-time web applications
- streamable-http: HTTP-based streaming protocol
Example
Basic server initialization:
server = DNALLMMCPServer("config/server_config.yaml")
await server.initialize()
server.start_server(host="127.0.0.1", port=8000, transport="sse")
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 SSE 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 SSE transport
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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 |
|
Functions¶
get_server_info ¶
get_server_info()
Get server information.
Source code in dnallm/mcp/server.py
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 |
|
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
142 143 144 145 146 147 148 149 150 151 152 153 154 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 |
|
shutdown
async
¶
shutdown()
Shutdown the server and cleanup resources.
Source code in dnallm/mcp/server.py
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 |
|
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
- sse: Server-Sent Events for real-time web applications
- streamable-http: HTTP-based streaming for REST API integration
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", "sse", "streamable-http". 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 SSE for real-time web apps
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
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 |
|
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
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 |
|
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
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 |
|