Spaces:
Sleeping
Sleeping
Update biopython/mcp_output/mcp_plugin/mcp_service.py
Browse files
biopython/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
|
|
|
| 3 |
from io import StringIO
|
| 4 |
|
| 5 |
# Ensure Biopython source is importable when running the MCP service
|
|
@@ -9,29 +11,42 @@ if source_path not in sys.path:
|
|
| 9 |
|
| 10 |
from fastmcp import FastMCP
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
from Bio import __version__ as bio_version
|
| 36 |
from Bio import Entrez, SeqIO, pairwise2, Phylo, AlignIO
|
| 37 |
from Bio.PDB import PDBParser
|
|
@@ -72,7 +87,10 @@ def bio_version_tool(payload: dict | None = None):
|
|
| 72 |
@mcp.tool(name="query_pubmed_print_usage", description="Print usage for query_pubmed script")
|
| 73 |
def print_usage_tool(payload: dict | None = None):
|
| 74 |
try:
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
return {"success": True, "result": result, "error": None}
|
| 77 |
except Exception as e:
|
| 78 |
return {"success": False, "result": None, "error": str(e)}
|
|
@@ -80,7 +98,10 @@ def print_usage_tool(payload: dict | None = None):
|
|
| 80 |
@mcp.tool(name="line_wrap", description="Wrap long strings (from update_ncbi_codon_table.py)")
|
| 81 |
def line_wrap_tool(payload: dict):
|
| 82 |
try:
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
return {"success": True, "result": result, "error": None}
|
| 85 |
except Exception as e:
|
| 86 |
return {"success": False, "result": None, "error": str(e)}
|
|
@@ -88,7 +109,10 @@ def line_wrap_tool(payload: dict):
|
|
| 88 |
@mcp.tool(name="scop_main", description="Extract SCOP domain PDB records (Scripts/scop_pdb.py main)")
|
| 89 |
def scop_main_tool(payload: dict | None = None):
|
| 90 |
try:
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
return {"success": True, "result": result, "error": None}
|
| 93 |
except Exception as e:
|
| 94 |
return {"success": False, "result": None, "error": str(e)}
|
|
@@ -96,7 +120,10 @@ def scop_main_tool(payload: dict | None = None):
|
|
| 96 |
@mcp.tool(name="scop_open_pdb", description="Download/open a PDB file (Scripts/scop_pdb.py open_pdb)")
|
| 97 |
def open_pdb_tool(payload: dict):
|
| 98 |
try:
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
| 100 |
return {"success": True, "result": str(result), "error": None}
|
| 101 |
except Exception as e:
|
| 102 |
return {"success": False, "result": None, "error": str(e)}
|
|
@@ -104,7 +131,10 @@ def open_pdb_tool(payload: dict):
|
|
| 104 |
@mcp.tool(name="scop_usage", description="Print usage for scop_pdb.py")
|
| 105 |
def usage_tool(payload: dict | None = None):
|
| 106 |
try:
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
return {"success": True, "result": result, "error": None}
|
| 109 |
except Exception as e:
|
| 110 |
return {"success": False, "result": None, "error": str(e)}
|
|
@@ -113,8 +143,9 @@ def usage_tool(payload: dict | None = None):
|
|
| 113 |
def notepad_tool(*args, **kwargs):
|
| 114 |
"""NotePad class"""
|
| 115 |
try:
|
|
|
|
| 116 |
if NotePad is None:
|
| 117 |
-
return {"success": False, "result": None, "error": "Class NotePad is not available
|
| 118 |
|
| 119 |
# MCP parameter type conversion
|
| 120 |
converted_args = []
|
|
@@ -123,7 +154,6 @@ def notepad_tool(*args, **kwargs):
|
|
| 123 |
# Handle position argument type conversion
|
| 124 |
for arg in args:
|
| 125 |
if isinstance(arg, str):
|
| 126 |
-
# Try to convert to numeric type
|
| 127 |
try:
|
| 128 |
if '.' in arg:
|
| 129 |
converted_args.append(float(arg))
|
|
@@ -153,7 +183,10 @@ def notepad_tool(*args, **kwargs):
|
|
| 153 |
@mcp.tool(name="nextorf_help", description="Help text for NextOrf (Scripts/xbbtools/nextorf.py)")
|
| 154 |
def help_tool(payload: dict | None = None):
|
| 155 |
try:
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
return {"success": True, "result": result, "error": None}
|
| 158 |
except Exception as e:
|
| 159 |
return {"success": False, "result": None, "error": str(e)}
|
|
@@ -161,7 +194,10 @@ def help_tool(payload: dict | None = None):
|
|
| 161 |
@mcp.tool(name="makeTableX", description="Generate NextOrf lookup table (Scripts/xbbtools/nextorf.py)")
|
| 162 |
def makeTableX_tool(payload: dict):
|
| 163 |
try:
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
return {"success": True, "result": result, "error": None}
|
| 166 |
except Exception as e:
|
| 167 |
return {"success": False, "result": None, "error": str(e)}
|
|
@@ -170,17 +206,16 @@ def makeTableX_tool(payload: dict):
|
|
| 170 |
def missingtable_tool(*args, **kwargs):
|
| 171 |
"""MissingTable class"""
|
| 172 |
try:
|
|
|
|
| 173 |
if MissingTable is None:
|
| 174 |
-
return {"success": False, "result": None, "error": "Class MissingTable is not available
|
| 175 |
|
| 176 |
# MCP parameter type conversion
|
| 177 |
converted_args = []
|
| 178 |
converted_kwargs = kwargs.copy()
|
| 179 |
|
| 180 |
-
# Handle position argument type conversion
|
| 181 |
for arg in args:
|
| 182 |
if isinstance(arg, str):
|
| 183 |
-
# Try to convert to numeric type
|
| 184 |
try:
|
| 185 |
if '.' in arg:
|
| 186 |
converted_args.append(float(arg))
|
|
@@ -191,7 +226,6 @@ def missingtable_tool(*args, **kwargs):
|
|
| 191 |
else:
|
| 192 |
converted_args.append(arg)
|
| 193 |
|
| 194 |
-
# Handle keyword argument type conversion
|
| 195 |
for key, value in converted_kwargs.items():
|
| 196 |
if isinstance(value, str):
|
| 197 |
try:
|
|
@@ -211,17 +245,16 @@ def missingtable_tool(*args, **kwargs):
|
|
| 211 |
def nextorf_tool(*args, **kwargs):
|
| 212 |
"""NextOrf class"""
|
| 213 |
try:
|
|
|
|
| 214 |
if NextOrf is None:
|
| 215 |
-
return {"success": False, "result": None, "error": "Class NextOrf is not available
|
| 216 |
|
| 217 |
# MCP parameter type conversion
|
| 218 |
converted_args = []
|
| 219 |
converted_kwargs = kwargs.copy()
|
| 220 |
|
| 221 |
-
# Handle position argument type conversion
|
| 222 |
for arg in args:
|
| 223 |
if isinstance(arg, str):
|
| 224 |
-
# Try to convert to numeric type
|
| 225 |
try:
|
| 226 |
if '.' in arg:
|
| 227 |
converted_args.append(float(arg))
|
|
@@ -232,7 +265,6 @@ def nextorf_tool(*args, **kwargs):
|
|
| 232 |
else:
|
| 233 |
converted_args.append(arg)
|
| 234 |
|
| 235 |
-
# Handle keyword argument type conversion
|
| 236 |
for key, value in converted_kwargs.items():
|
| 237 |
if isinstance(value, str):
|
| 238 |
try:
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import ast
|
| 4 |
+
import importlib
|
| 5 |
from io import StringIO
|
| 6 |
|
| 7 |
# Ensure Biopython source is importable when running the MCP service
|
|
|
|
| 11 |
|
| 12 |
from fastmcp import FastMCP
|
| 13 |
|
| 14 |
+
# Lightweight stand-ins for setup.py utilities to avoid running setup()
|
| 15 |
+
def biopy_can_import(module_name):
|
| 16 |
+
try:
|
| 17 |
+
return importlib.import_module(module_name)
|
| 18 |
+
except ImportError:
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def biopy_get_version():
|
| 23 |
+
init_path = os.path.join(source_path, "Bio", "__init__.py")
|
| 24 |
+
try:
|
| 25 |
+
with open(init_path, "r", encoding="utf-8") as handle:
|
| 26 |
+
for line in handle:
|
| 27 |
+
if line.startswith("__version__ = "):
|
| 28 |
+
return ast.literal_eval(line.split("=", 1)[1].strip())
|
| 29 |
+
except FileNotFoundError:
|
| 30 |
+
return "Unknown"
|
| 31 |
+
return "Unknown"
|
| 32 |
+
|
| 33 |
+
# ✅ 延迟导入:只在需要时才导入 Scripts 模块
|
| 34 |
+
# 这样可以避免在服务启动时触发 setuptools
|
| 35 |
+
_scripts_cache = {}
|
| 36 |
+
|
| 37 |
+
def lazy_import_script(module_path, attr_name):
|
| 38 |
+
"""延迟导入 Scripts 模块中的函数或类"""
|
| 39 |
+
cache_key = f"{module_path}.{attr_name}"
|
| 40 |
+
if cache_key not in _scripts_cache:
|
| 41 |
+
try:
|
| 42 |
+
module = importlib.import_module(module_path)
|
| 43 |
+
_scripts_cache[cache_key] = getattr(module, attr_name)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"⚠️ Warning: Could not import {cache_key}: {e}")
|
| 46 |
+
_scripts_cache[cache_key] = None
|
| 47 |
+
return _scripts_cache[cache_key]
|
| 48 |
+
|
| 49 |
+
# Biopython top-level imports (这些是安全的)
|
| 50 |
from Bio import __version__ as bio_version
|
| 51 |
from Bio import Entrez, SeqIO, pairwise2, Phylo, AlignIO
|
| 52 |
from Bio.PDB import PDBParser
|
|
|
|
| 87 |
@mcp.tool(name="query_pubmed_print_usage", description="Print usage for query_pubmed script")
|
| 88 |
def print_usage_tool(payload: dict | None = None):
|
| 89 |
try:
|
| 90 |
+
func = lazy_import_script("Scripts.query_pubmed", "print_usage")
|
| 91 |
+
if func is None:
|
| 92 |
+
return {"success": False, "result": None, "error": "Function not available"}
|
| 93 |
+
result = func()
|
| 94 |
return {"success": True, "result": result, "error": None}
|
| 95 |
except Exception as e:
|
| 96 |
return {"success": False, "result": None, "error": str(e)}
|
|
|
|
| 98 |
@mcp.tool(name="line_wrap", description="Wrap long strings (from update_ncbi_codon_table.py)")
|
| 99 |
def line_wrap_tool(payload: dict):
|
| 100 |
try:
|
| 101 |
+
func = lazy_import_script("Scripts.update_ncbi_codon_table", "line_wrap")
|
| 102 |
+
if func is None:
|
| 103 |
+
return {"success": False, "result": None, "error": "Function not available"}
|
| 104 |
+
result = func(**payload)
|
| 105 |
return {"success": True, "result": result, "error": None}
|
| 106 |
except Exception as e:
|
| 107 |
return {"success": False, "result": None, "error": str(e)}
|
|
|
|
| 109 |
@mcp.tool(name="scop_main", description="Extract SCOP domain PDB records (Scripts/scop_pdb.py main)")
|
| 110 |
def scop_main_tool(payload: dict | None = None):
|
| 111 |
try:
|
| 112 |
+
func = lazy_import_script("Scripts.scop_pdb", "main")
|
| 113 |
+
if func is None:
|
| 114 |
+
return {"success": False, "result": None, "error": "Function not available"}
|
| 115 |
+
result = func()
|
| 116 |
return {"success": True, "result": result, "error": None}
|
| 117 |
except Exception as e:
|
| 118 |
return {"success": False, "result": None, "error": str(e)}
|
|
|
|
| 120 |
@mcp.tool(name="scop_open_pdb", description="Download/open a PDB file (Scripts/scop_pdb.py open_pdb)")
|
| 121 |
def open_pdb_tool(payload: dict):
|
| 122 |
try:
|
| 123 |
+
func = lazy_import_script("Scripts.scop_pdb", "open_pdb")
|
| 124 |
+
if func is None:
|
| 125 |
+
return {"success": False, "result": None, "error": "Function not available"}
|
| 126 |
+
result = func(**payload)
|
| 127 |
return {"success": True, "result": str(result), "error": None}
|
| 128 |
except Exception as e:
|
| 129 |
return {"success": False, "result": None, "error": str(e)}
|
|
|
|
| 131 |
@mcp.tool(name="scop_usage", description="Print usage for scop_pdb.py")
|
| 132 |
def usage_tool(payload: dict | None = None):
|
| 133 |
try:
|
| 134 |
+
func = lazy_import_script("Scripts.scop_pdb", "usage")
|
| 135 |
+
if func is None:
|
| 136 |
+
return {"success": False, "result": None, "error": "Function not available"}
|
| 137 |
+
result = func()
|
| 138 |
return {"success": True, "result": result, "error": None}
|
| 139 |
except Exception as e:
|
| 140 |
return {"success": False, "result": None, "error": str(e)}
|
|
|
|
| 143 |
def notepad_tool(*args, **kwargs):
|
| 144 |
"""NotePad class"""
|
| 145 |
try:
|
| 146 |
+
NotePad = lazy_import_script("Scripts.xbbtools.xbb_utils", "NotePad")
|
| 147 |
if NotePad is None:
|
| 148 |
+
return {"success": False, "result": None, "error": "Class NotePad is not available"}
|
| 149 |
|
| 150 |
# MCP parameter type conversion
|
| 151 |
converted_args = []
|
|
|
|
| 154 |
# Handle position argument type conversion
|
| 155 |
for arg in args:
|
| 156 |
if isinstance(arg, str):
|
|
|
|
| 157 |
try:
|
| 158 |
if '.' in arg:
|
| 159 |
converted_args.append(float(arg))
|
|
|
|
| 183 |
@mcp.tool(name="nextorf_help", description="Help text for NextOrf (Scripts/xbbtools/nextorf.py)")
|
| 184 |
def help_tool(payload: dict | None = None):
|
| 185 |
try:
|
| 186 |
+
func = lazy_import_script("Scripts.xbbtools.nextorf", "help")
|
| 187 |
+
if func is None:
|
| 188 |
+
return {"success": False, "result": None, "error": "Function not available"}
|
| 189 |
+
result = func()
|
| 190 |
return {"success": True, "result": result, "error": None}
|
| 191 |
except Exception as e:
|
| 192 |
return {"success": False, "result": None, "error": str(e)}
|
|
|
|
| 194 |
@mcp.tool(name="makeTableX", description="Generate NextOrf lookup table (Scripts/xbbtools/nextorf.py)")
|
| 195 |
def makeTableX_tool(payload: dict):
|
| 196 |
try:
|
| 197 |
+
func = lazy_import_script("Scripts.xbbtools.nextorf", "makeTableX")
|
| 198 |
+
if func is None:
|
| 199 |
+
return {"success": False, "result": None, "error": "Function not available"}
|
| 200 |
+
result = func(**payload)
|
| 201 |
return {"success": True, "result": result, "error": None}
|
| 202 |
except Exception as e:
|
| 203 |
return {"success": False, "result": None, "error": str(e)}
|
|
|
|
| 206 |
def missingtable_tool(*args, **kwargs):
|
| 207 |
"""MissingTable class"""
|
| 208 |
try:
|
| 209 |
+
MissingTable = lazy_import_script("Scripts.xbbtools.nextorf", "MissingTable")
|
| 210 |
if MissingTable is None:
|
| 211 |
+
return {"success": False, "result": None, "error": "Class MissingTable is not available"}
|
| 212 |
|
| 213 |
# MCP parameter type conversion
|
| 214 |
converted_args = []
|
| 215 |
converted_kwargs = kwargs.copy()
|
| 216 |
|
|
|
|
| 217 |
for arg in args:
|
| 218 |
if isinstance(arg, str):
|
|
|
|
| 219 |
try:
|
| 220 |
if '.' in arg:
|
| 221 |
converted_args.append(float(arg))
|
|
|
|
| 226 |
else:
|
| 227 |
converted_args.append(arg)
|
| 228 |
|
|
|
|
| 229 |
for key, value in converted_kwargs.items():
|
| 230 |
if isinstance(value, str):
|
| 231 |
try:
|
|
|
|
| 245 |
def nextorf_tool(*args, **kwargs):
|
| 246 |
"""NextOrf class"""
|
| 247 |
try:
|
| 248 |
+
NextOrf = lazy_import_script("Scripts.xbbtools.nextorf", "NextOrf")
|
| 249 |
if NextOrf is None:
|
| 250 |
+
return {"success": False, "result": None, "error": "Class NextOrf is not available"}
|
| 251 |
|
| 252 |
# MCP parameter type conversion
|
| 253 |
converted_args = []
|
| 254 |
converted_kwargs = kwargs.copy()
|
| 255 |
|
|
|
|
| 256 |
for arg in args:
|
| 257 |
if isinstance(arg, str):
|
|
|
|
| 258 |
try:
|
| 259 |
if '.' in arg:
|
| 260 |
converted_args.append(float(arg))
|
|
|
|
| 265 |
else:
|
| 266 |
converted_args.append(arg)
|
| 267 |
|
|
|
|
| 268 |
for key, value in converted_kwargs.items():
|
| 269 |
if isinstance(value, str):
|
| 270 |
try:
|