guohanghui commited on
Commit
2173366
·
verified ·
1 Parent(s): 6a4bb83

Update biopython/mcp_output/mcp_plugin/mcp_service.py

Browse files
biopython/mcp_output/mcp_plugin/mcp_service.py CHANGED
@@ -6,8 +6,14 @@ sys.path.insert(0, source_path)
6
 
7
  from fastmcp import FastMCP
8
  from Bio.SeqIO import parse, read, write
 
 
9
  from Bio.Blast.NCBIWWW import qblast
10
- from Bio.Entrez import efetch, esearch
 
 
 
 
11
 
12
  mcp = FastMCP("biopython_service")
13
 
@@ -30,6 +36,25 @@ def seqrecord_to_dict(record):
30
  ] if record.features else []
31
  }
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  @mcp.tool(name="seqio_parse", description="Parse sequence data from a file.")
34
  def seqio_parse(file_path: str, format: str) -> dict:
35
  """
@@ -76,7 +101,7 @@ def seqio_write(sequences, file_path: str, format: str) -> dict:
76
  Writes sequences to a file.
77
 
78
  Parameters:
79
- - sequences: List of sequences to write.
80
  - file_path: Path to the output file.
81
  - format: Format of the output file (e.g., 'fasta').
82
 
@@ -84,7 +109,22 @@ def seqio_write(sequences, file_path: str, format: str) -> dict:
84
  - A dictionary with success status and number of records written or error message.
85
  """
86
  try:
87
- count = write(sequences, file_path, format)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  return {"success": True, "result": count, "error": None}
89
  except Exception as e:
90
  return {"success": False, "result": None, "error": str(e)}
@@ -134,15 +174,32 @@ def entrez_esearch(db: str, term: str) -> dict:
134
  Searches NCBI's Entrez databases.
135
 
136
  Parameters:
137
- - db: Database to search (e.g., 'pubmed').
138
- - term: Search term.
 
 
 
 
139
 
140
  Returns:
141
  - A dictionary with success status and search results or error message.
 
 
 
 
 
142
  """
143
  try:
144
- handle = esearch(db=db, term=term)
145
- return {"success": True, "result": handle.read(), "error": None}
 
 
 
 
 
 
 
 
146
  except Exception as e:
147
  return {"success": False, "result": None, "error": str(e)}
148
 
 
6
 
7
  from fastmcp import FastMCP
8
  from Bio.SeqIO import parse, read, write
9
+ from Bio.Seq import Seq
10
+ from Bio.SeqRecord import SeqRecord
11
  from Bio.Blast.NCBIWWW import qblast
12
+ from Bio.Entrez import efetch, esearch, email
13
+ import Bio.Entrez
14
+
15
+ # 设置 NCBI Entrez email(必需)
16
+ Bio.Entrez.email = "biopython-mcp@huggingface.co"
17
 
18
  mcp = FastMCP("biopython_service")
19
 
 
36
  ] if record.features else []
37
  }
38
 
39
+ def dict_to_seqrecord(seq_dict):
40
+ """Convert a dictionary back to a SeqRecord object."""
41
+ if isinstance(seq_dict, SeqRecord):
42
+ return seq_dict
43
+
44
+ # 从字典创建 SeqRecord
45
+ record = SeqRecord(
46
+ Seq(seq_dict.get("sequence", "")),
47
+ id=seq_dict.get("id", ""),
48
+ name=seq_dict.get("name", ""),
49
+ description=seq_dict.get("description", "")
50
+ )
51
+
52
+ # 添加注释
53
+ if "annotations" in seq_dict:
54
+ record.annotations.update(seq_dict["annotations"])
55
+
56
+ return record
57
+
58
  @mcp.tool(name="seqio_parse", description="Parse sequence data from a file.")
59
  def seqio_parse(file_path: str, format: str) -> dict:
60
  """
 
101
  Writes sequences to a file.
102
 
103
  Parameters:
104
+ - sequences: List of sequences to write (can be SeqRecord objects or dicts).
105
  - file_path: Path to the output file.
106
  - format: Format of the output file (e.g., 'fasta').
107
 
 
109
  - A dictionary with success status and number of records written or error message.
110
  """
111
  try:
112
+ # 如果 sequences 是列表,将每个元素转换为 SeqRecord
113
+ if isinstance(sequences, list):
114
+ records = []
115
+ for seq in sequences:
116
+ if isinstance(seq, dict):
117
+ records.append(dict_to_seqrecord(seq))
118
+ else:
119
+ records.append(seq)
120
+ else:
121
+ # 如果是单个对象
122
+ if isinstance(sequences, dict):
123
+ records = [dict_to_seqrecord(sequences)]
124
+ else:
125
+ records = [sequences]
126
+
127
+ count = write(records, file_path, format)
128
  return {"success": True, "result": count, "error": None}
129
  except Exception as e:
130
  return {"success": False, "result": None, "error": str(e)}
 
174
  Searches NCBI's Entrez databases.
175
 
176
  Parameters:
177
+ - db: Database to search (e.g., 'nucleotide', 'protein', 'gene', 'pubmed').
178
+ - term: Search term. Use proper NCBI query syntax:
179
+ - gene_name[GENE] - Search by gene name
180
+ - organism[ORGN] - Search by organism
181
+ - "Homo sapiens"[ORGN] - Species filter
182
+ - "RefSeq"[Filter] - Only RefSeq records
183
 
184
  Returns:
185
  - A dictionary with success status and search results or error message.
186
+
187
+ Examples:
188
+ - "TP53[gene] AND human[organism]"
189
+ - "BRCA1[gene]"
190
+ - "insulin[protein name]"
191
  """
192
  try:
193
+ # 确保 email 已设置
194
+ if not Bio.Entrez.email:
195
+ Bio.Entrez.email = "biopython-mcp@huggingface.co"
196
+
197
+ # 使用 retmax 参数获取更多结果
198
+ handle = esearch(db=db, term=term, retmax=100)
199
+ result = handle.read()
200
+ handle.close()
201
+
202
+ return {"success": True, "result": result, "error": None}
203
  except Exception as e:
204
  return {"success": False, "result": None, "error": str(e)}
205