Learn how to use the FlashMCP Client to interact with MCP servers.
New in version: 2.0.0
The FlashMCP.Client
provides a high-level, asynchronous interface for interacting with any Model Context Protocol (MCP) server, whether it’s built with FlashMCP or another implementation. It simplifies communication by handling protocol details and connection management.
Client
) from the connection mechanism (Transport
).
Client
: Handles sending MCP requests (like tools/call
, resources/read
), receiving responses, and managing callbacks.Transport
: Responsible for establishing and maintaining the connection to the server (e.g., via WebSockets, SSE, Stdio, or in-memory).transport
. You can either provide an already instantiated transport object, or provide a transport source and let FlashMCP attempt to infer the correct transport to use.
The following inference rules are used to determine the appropriate ClientTransport
based on the input type:
ClientTransport
Instance: If you provide an already instantiated transport object, it’s used directly.FlashMCP
Instance: Creates a FlashMCPTransport
for efficient in-memory communication (ideal for testing).Path
or str
pointing to an existing file:
.py
: Creates a PythonStdioTransport
to run the script using python
..js
: Creates a NodeStdioTransport
to run the script using node
.AnyUrl
or str
pointing to a URL that begins with http://
or https://
:
StreamableHttpTransport
MCPConfig
or dictionary matching MCPConfig schema: Creates a client that connects to one or more MCP servers specified in the config.ValueError
if the type cannot be inferred.MCPConfig
file:
ClientTransport
class yourself and pass it to the Client
. See the Transports page for details.New in version: 2.3.6
FlashMCP supports creating clients that connect to multiple MCP servers through a single client interface using a standard MCP configuration format (MCPConfig
). This configuration approach makes it easy to connect to multiple specialized servers or create composable systems with a simple, declarative syntax.
MCPConfig
containing multiple servers:
servername_toolname
and protocol://servername/resource/path
async with
block. This context manager handles establishing the connection, initializing the MCP session, and cleaning up resources upon exit.
async with
block using the established session.
Client
provides methods corresponding to standard MCP requests:
*_mcp
methods described later.list_tools()
: Retrieves a list of tools available on the server.
call_tool(name: str, arguments: dict[str, Any] | None = None, timeout: float | None = None, progress_handler: ProgressHandler | None = None)
: Executes a tool on the server.
TextContent
or ImageContent
).timeout
parameter limits the maximum execution time (in seconds) for this specific call, overriding any client-level timeout.progress_handler
parameter receives progress updates during execution, overriding any client-level progress handler.list_resources()
: Retrieves a list of static resources.
list_resource_templates()
: Retrieves a list of resource templates.
read_resource(uri: str | AnyUrl)
: Reads the content of a resource or a resolved template.
list_prompts()
: Retrieves available prompt templates.get_prompt(name: str, arguments: dict[str, Any] | None = None)
: Retrieves a rendered prompt message list.New in version: 2.2.7
The FlashMCP client attempts to provide a “friendly” interface to the MCP protocol, but sometimes you may need access to the raw MCP protocol objects. Each of the main client methods that returns data has a corresponding *_mcp
method that returns the raw MCP protocol objects directly.
_mcp
) return user-friendly representations of MCP data, while *_mcp
methods will always return the complete MCP protocol objects. As the protocol evolves, changes to these user-friendly representations may occur and could potentially be breaking. If you need consistent, stable access to the full data structure, prefer using the *_mcp
methods.list_tools_mcp()
: Returns mcp.types.ListToolsResult
call_tool_mcp(name, arguments)
: Returns mcp.types.CallToolResult
list_resources_mcp()
: Returns mcp.types.ListResourcesResult
list_resource_templates_mcp()
: Returns mcp.types.ListResourceTemplatesResult
read_resource_mcp(uri)
: Returns mcp.types.ReadResourceResult
list_prompts_mcp()
: Returns mcp.types.ListPromptsResult
get_prompt_mcp(name, arguments)
: Returns mcp.types.GetPromptResult
complete_mcp(ref, argument)
: Returns mcp.types.CompleteResult
New in version: 2.3.4
You can control request timeouts at both the client level and individual request level:
call_tool
request results in an error on the server (e.g., the tool function raised an exception), the client.call_tool()
method will raise a FlashMCP.client.ClientError
.
ConnectionError
, TimeoutError
).
call_tool
outside of the async with
block. Instead, you can use call_tool_mcp()
to get the raw mcp.types.CallToolResult
object and handle errors yourself by checking its isError
attribute.