44Each server demonstrates a single feature and can be run as a standalone server.
55"""
66
7+ import importlib
78import sys
89from typing import Literal
910
11+ # Available snippet modules
12+ SNIPPET_MODULES = [
13+ "basic_tool" ,
14+ "basic_resource" ,
15+ "basic_prompt" ,
16+ "tool_progress" ,
17+ "sampling" ,
18+ "elicitation" ,
19+ "completion" ,
20+ "notifications" ,
21+ ]
22+
1023
1124def run_server (module_name : str , transport : Literal ["stdio" , "sse" , "streamable-http" ] | None = None ) -> None :
1225 """Run a snippet server with the specified transport.
@@ -16,41 +29,13 @@ def run_server(module_name: str, transport: Literal["stdio", "sse", "streamable-
1629 transport: Transport to use (stdio, sse, streamable-http).
1730 If None, uses first command line arg or defaults to stdio.
1831 """
19- # Import the specific module based on name
20- if module_name == "basic_tool" :
21- from . import basic_tool
22-
23- mcp = basic_tool .mcp
24- elif module_name == "basic_resource" :
25- from . import basic_resource
26-
27- mcp = basic_resource .mcp
28- elif module_name == "basic_prompt" :
29- from . import basic_prompt
30-
31- mcp = basic_prompt .mcp
32- elif module_name == "tool_progress" :
33- from . import tool_progress
34-
35- mcp = tool_progress .mcp
36- elif module_name == "sampling" :
37- from . import sampling
32+ # Validate module name
33+ if module_name not in SNIPPET_MODULES :
34+ raise ValueError (f"Unknown module: { module_name } . Available modules: { ', ' .join (SNIPPET_MODULES )} " )
3835
39- mcp = sampling .mcp
40- elif module_name == "elicitation" :
41- from . import elicitation
42-
43- mcp = elicitation .mcp
44- elif module_name == "completion" :
45- from . import completion
46-
47- mcp = completion .mcp
48- elif module_name == "notifications" :
49- from . import notifications
50-
51- mcp = notifications .mcp
52- else :
53- raise ValueError (f"Unknown module: { module_name } " )
36+ # Import the module dynamically
37+ module = importlib .import_module (f".{ module_name } " , package = __name__ )
38+ mcp = module .mcp
5439
5540 # Determine transport
5641 if transport is None :
@@ -65,42 +50,18 @@ def run_server(module_name: str, transport: Literal["stdio", "sse", "streamable-
6550 mcp .run (transport = transport ) # type: ignore
6651
6752
68- # Entry points for each snippet
69- def run_basic_tool ():
70- """Run the basic tool example server."""
71- run_server ("basic_tool" )
72-
73-
74- def run_basic_resource ():
75- """Run the basic resource example server."""
76- run_server ("basic_resource" )
77-
78-
79- def run_basic_prompt ():
80- """Run the basic prompt example server."""
81- run_server ("basic_prompt" )
82-
83-
84- def run_tool_progress ():
85- """Run the tool progress example server."""
86- run_server ("tool_progress" )
87-
88-
89- def run_sampling ():
90- """Run the sampling example server."""
91- run_server ("sampling" )
92-
93-
94- def run_elicitation ():
95- """Run the elicitation example server."""
96- run_server ("elicitation" )
53+ # Create entry point functions dynamically
54+ def _create_run_function (module_name : str ):
55+ """Create a run function for a specific module."""
9756
57+ def run_function ():
58+ f"""Run the { module_name .replace ('_' , ' ' )} example server."""
59+ run_server (module_name )
9860
99- def run_completion ():
100- """Run the completion example server."""
101- run_server ("completion" )
61+ return run_function
10262
10363
104- def run_notifications ():
105- """Run the notifications example server."""
106- run_server ("notifications" )
64+ # Generate entry points for each snippet
65+ for module in SNIPPET_MODULES :
66+ func_name = f"run_{ module } "
67+ globals ()[func_name ] = _create_run_function (module )
0 commit comments