MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / MCPServer

Class MCPServer

src/mcp/server/mcpserver/server.py:130–1099  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128
129
130class MCPServer(Generic[LifespanResultT]):
131 def __init__(
132 self,
133 name: str | None = None,
134 title: str | None = None,
135 description: str | None = None,
136 instructions: str | None = None,
137 website_url: str | None = None,
138 icons: list[Icon] | None = None,
139 version: str | None = None,
140 auth_server_provider: OAuthAuthorizationServerProvider[Any, Any, Any] | None = None,
141 token_verifier: TokenVerifier | None = None,
142 *,
143 tools: list[Tool] | None = None,
144 resources: list[Resource] | None = None,
145 debug: bool = False,
146 log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO",
147 warn_on_duplicate_resources: bool = True,
148 warn_on_duplicate_tools: bool = True,
149 warn_on_duplicate_prompts: bool = True,
150 dependencies: list[str] | None = None,
151 lifespan: Callable[[MCPServer[LifespanResultT]], AbstractAsyncContextManager[LifespanResultT]] | None = None,
152 auth: AuthSettings | None = None,
153 ):
154 self.settings = Settings(
155 debug=debug,
156 log_level=log_level,
157 warn_on_duplicate_resources=warn_on_duplicate_resources,
158 warn_on_duplicate_tools=warn_on_duplicate_tools,
159 warn_on_duplicate_prompts=warn_on_duplicate_prompts,
160 dependencies=dependencies or [],
161 lifespan=lifespan,
162 auth=auth,
163 )
164 self.dependencies = self.settings.dependencies
165
166 self._tool_manager = ToolManager(tools=tools, warn_on_duplicate_tools=self.settings.warn_on_duplicate_tools)
167 self._resource_manager = ResourceManager(
168 resources=resources, warn_on_duplicate_resources=self.settings.warn_on_duplicate_resources
169 )
170 self._prompt_manager = PromptManager(warn_on_duplicate_prompts=self.settings.warn_on_duplicate_prompts)
171 self._lowlevel_server = Server(
172 name=name or "mcp-server",
173 title=title,
174 description=description,
175 instructions=instructions,
176 website_url=website_url,
177 icons=icons,
178 version=version,
179 on_list_tools=self._handle_list_tools,
180 on_call_tool=self._handle_call_tool,
181 on_list_resources=self._handle_list_resources,
182 on_read_resource=self._handle_read_resource,
183 on_list_resource_templates=self._handle_list_resource_templates,
184 on_list_prompts=self._handle_list_prompts,
185 on_get_prompt=self._handle_get_prompt,
186 # TODO(Marcelo): It seems there's a type mismatch between the lifespan type from an MCPServer and Server.
187 # We need to create a Lifespan type that is a generic on the server type, like Starlette does.

Calls

no outgoing calls