77import os
88import shutil
99import sys
10+ import argparse
1011from pathlib import Path
1112from mcp .server .fastmcp import FastMCP
1213
14+ # 解析命令行参数
15+ def parse_arguments ():
16+ parser = argparse .ArgumentParser (description = 'MCP Server for converting Markdown to mindmaps' )
17+ parser .add_argument ('--return-type' ,choices = ['html' ,'filePath' ],default = 'html' ,
18+ help = 'Whether to return HTML content or file path. Default: html' )
19+ return parser .parse_args ()
20+
21+ # 全局配置
22+ args = parse_arguments ()
23+ RETURN_TYPE = args .return_type
24+
1325# Initialize FastMCP server
1426mcp = FastMCP ("mindmap-server" )
1527
@@ -23,21 +35,15 @@ async def create_temp_file(content: str, extension: str) -> str:
2335
2436return file_path
2537
26- async def run_mindmap (input_file :str ,output_file :str = None , offline : bool = False , no_toolbar : bool = False )-> str :
27- """Runmaindmap markmap-cli on the input file and return the path to the output file."""
38+ async def run_mindmap (input_file :str ,output_file :str = None )-> str :
39+ """Run markmap-cli on the input file and return the path to the output file."""
2840args = ['npx' ,'-y' ,'markmap-cli' ,input_file ,'--no-open' ]
2941
3042if output_file :
3143args .extend (['-o' ,output_file ])
3244else :
3345output_file = os .path .splitext (input_file )[0 ]+ '.html'
3446
35- if offline :
36- args .append ('--offline' )
37-
38- if no_toolbar :
39- args .append ('--no-toolbar' )
40-
4147try :
4248process = await asyncio .create_subprocess_exec (
4349* args ,
@@ -63,34 +69,29 @@ async def get_html_content(file_path: str) -> str:
6369@mcp .tool ()
6470async def convert_markdown_to_mindmap (
6571markdown_content :str ,# The Markdown content to convert
66- return_type :str ,# Whether to return 'html' content or 'filePath'
67- offline :bool = False ,# Generate offline-capable HTML with all assets inlined
68- no_toolbar :bool = False # Hide the toolbar in the generated mindmap
6972)-> str :
7073"""Convert Markdown content to a mindmap mind map.
7174
7275 Args:
7376 markdown_content: The Markdown content to convert
74- return_type: Either 'html' to return the HTML content, or 'filePath' to return the file path
75- offline: Whether to generate offline-capable HTML with all assets inlined
76- no_toolbar: Whether to hide the toolbar in the generated mindmap
7777
7878 Returns:
79- Either the HTML content or the file path to the generated HTML
79+ Either the HTML content or the file path to the generated HTML,
80+ depending on the --return-type server argument
8081 """
8182try :
8283# Create a temporary markdown file
8384input_file = await create_temp_file (markdown_content ,'.md' )
8485
8586# Run mindmap on it
86- output_file = await run_mindmap (input_file , offline = offline , no_toolbar = no_toolbar )
87+ output_file = await run_mindmap (input_file )
8788
8889# Check if the output file exists
8990if not os .path .exists (output_file ):
9091raise RuntimeError (f"Output file was not created:{ output_file } " )
9192
92- # Return either the HTML content or the file path
93- if return_type == 'html' :
93+ # Return either the HTML content or the file path based on command line arg
94+ if RETURN_TYPE == 'html' :
9495html_content = await get_html_content (output_file )
9596return html_content
9697else :
@@ -100,8 +101,16 @@ async def convert_markdown_to_mindmap(
100101
101102def main ():
102103"""Entry point for the mindmap-mcp-server command."""
104+ global args ,RETURN_TYPE
105+
106+ # 再次解析参数以确保在作为入口点运行时也能获取参数
107+ args = parse_arguments ()
108+ RETURN_TYPE = args .return_type
109+
110+ print (f"Starting mindmap-mcp-server with return type:{ RETURN_TYPE } " ,file = sys .stderr )
111+
103112# Initialize and run the server
104113mcp .run (transport = 'stdio' )
105114
106115if __name__ == "__main__" :
107- main ()
116+ main ()