Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Guides creation of high-quality MCP servers in TypeScript or Python (FastMCP) to connect LLMs with external services.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/connections.py
1"""Lightweight connection handling for MCP servers."""23from abc import ABC, abstractmethod4from contextlib import AsyncExitStack5from typing import Any67from mcp import ClientSession, StdioServerParameters8from mcp.client.sse import sse_client9from mcp.client.stdio import stdio_client10from mcp.client.streamable_http import streamablehttp_client111213class MCPConnection(ABC):14"""Base class for MCP server connections."""1516def __init__(self):17self.session = None18self._stack = None1920@abstractmethod21def _create_context(self):22"""Create the connection context based on connection type."""2324async def __aenter__(self):25"""Initialize MCP server connection."""26self._stack = AsyncExitStack()27await self._stack.__aenter__()2829try:30ctx = self._create_context()31result = await self._stack.enter_async_context(ctx)3233if len(result) == 2:34read, write = result35elif len(result) == 3:36read, write, _ = result37else:38raise ValueError(f"Unexpected context result: {result}")3940session_ctx = ClientSession(read, write)41self.session = await self._stack.enter_async_context(session_ctx)42await self.session.initialize()43return self44except BaseException:45await self._stack.__aexit__(None, None, None)46raise4748async def __aexit__(self, exc_type, exc_val, exc_tb):49"""Clean up MCP server connection resources."""50if self._stack:51await self._stack.__aexit__(exc_type, exc_val, exc_tb)52self.session = None53self._stack = None5455async def list_tools(self) -> list[dict[str, Any]]:56"""Retrieve available tools from the MCP server."""57response = await self.session.list_tools()58return [59{60"name": tool.name,61"description": tool.description,62"input_schema": tool.inputSchema,63}64for tool in response.tools65]6667async def call_tool(self, tool_name: str, arguments: dict[str, Any]) -> Any:68"""Call a tool on the MCP server with provided arguments."""69result = await self.session.call_tool(tool_name, arguments=arguments)70return result.content717273class MCPConnectionStdio(MCPConnection):74"""MCP connection using standard input/output."""7576def __init__(self, command: str, args: list[str] = None, env: dict[str, str] = None):77super().__init__()78self.command = command79self.args = args or []80self.env = env8182def _create_context(self):83return stdio_client(84StdioServerParameters(command=self.command, args=self.args, env=self.env)85)868788class MCPConnectionSSE(MCPConnection):89"""MCP connection using Server-Sent Events."""9091def __init__(self, url: str, headers: dict[str, str] = None):92super().__init__()93self.url = url94self.headers = headers or {}9596def _create_context(self):97return sse_client(url=self.url, headers=self.headers)9899100class MCPConnectionHTTP(MCPConnection):101"""MCP connection using Streamable HTTP."""102103def __init__(self, url: str, headers: dict[str, str] = None):104super().__init__()105self.url = url106self.headers = headers or {}107108def _create_context(self):109return streamablehttp_client(url=self.url, headers=self.headers)110111112def create_connection(113transport: str,114command: str = None,115args: list[str] = None,116env: dict[str, str] = None,117url: str = None,118headers: dict[str, str] = None,119) -> MCPConnection:120"""Factory function to create the appropriate MCP connection.121122Args:123transport: Connection type ("stdio", "sse", or "http")124command: Command to run (stdio only)125args: Command arguments (stdio only)126env: Environment variables (stdio only)127url: Server URL (sse and http only)128headers: HTTP headers (sse and http only)129130Returns:131MCPConnection instance132"""133transport = transport.lower()134135if transport == "stdio":136if not command:137raise ValueError("Command is required for stdio transport")138return MCPConnectionStdio(command=command, args=args, env=env)139140elif transport == "sse":141if not url:142raise ValueError("URL is required for sse transport")143return MCPConnectionSSE(url=url, headers=headers)144145elif transport in ["http", "streamable_http", "streamable-http"]:146if not url:147raise ValueError("URL is required for http transport")148return MCPConnectionHTTP(url=url, headers=headers)149150else:151raise ValueError(f"Unsupported transport type: {transport}. Use 'stdio', 'sse', or 'http'")152