sessions
Connection = Union[StdioConnection, SSEConnection, StreamableHttpConnection, WebsocketConnection]
module-attribute
DEFAULT_ENCODING = 'utf-8'
module-attribute
DEFAULT_ENCODING_ERROR_HANDLER = 'strict'
module-attribute
DEFAULT_HTTP_TIMEOUT = 5
module-attribute
DEFAULT_SSE_READ_TIMEOUT = 60 * 5
module-attribute
DEFAULT_STREAMABLE_HTTP_SSE_READ_TIMEOUT = timedelta(seconds=60 * 5)
module-attribute
DEFAULT_STREAMABLE_HTTP_TIMEOUT = timedelta(seconds=30)
module-attribute
EncodingErrorHandler = Literal['strict', 'ignore', 'replace']
module-attribute
McpHttpClientFactory
Bases: Protocol
Source code in griptape/tools/mcp/sessions.py
SSEConnection
Bases: TypedDict
Source code in griptape/tools/mcp/sessions.py
headers
instance-attribute
HTTP headers to send to the SSE endpoint.
httpx_client_factory
instance-attribute
Custom factory for httpx.AsyncClient (optional).
session_kwargs
instance-attribute
Additional keyword arguments to pass to the ClientSession.
sse_read_timeout
instance-attribute
SSE read timeout.
timeout
instance-attribute
HTTP timeout.
transport
instance-attribute
url
instance-attribute
The URL of the SSE endpoint to connect to.
StdioConnection
Bases: TypedDict
Source code in griptape/tools/mcp/sessions.py
args
instance-attribute
Command line arguments to pass to the executable.
command
instance-attribute
The executable to run to start the server.
cwd
instance-attribute
The working directory to use when spawning the process.
encoding
instance-attribute
The text encoding used when sending/receiving messages to the server.
encoding_error_handler
instance-attribute
The text encoding error handler.
See https://docs.python.org/3/library/codecs.html#codec-base-classes for explanations of possible values.
env
instance-attribute
The environment to use when spawning the process.
session_kwargs
instance-attribute
Additional keyword arguments to pass to the ClientSession.
transport
instance-attribute
StreamableHttpConnection
Bases: TypedDict
Source code in griptape/tools/mcp/sessions.py
headers
instance-attribute
HTTP headers to send to the endpoint.
httpx_client_factory
instance-attribute
Custom factory for httpx.AsyncClient (optional).
session_kwargs
instance-attribute
Additional keyword arguments to pass to the ClientSession.
sse_read_timeout
instance-attribute
How long (in seconds) the client will wait for a new event before disconnecting.
All other HTTP operations are controlled by timeout.
terminate_on_close
instance-attribute
Whether to terminate the session on close.
timeout
instance-attribute
HTTP timeout.
transport
instance-attribute
url
instance-attribute
The URL of the endpoint to connect to.
WebsocketConnection
Bases: TypedDict
Source code in griptape/tools/mcp/sessions.py
session_kwargs
instance-attribute
Additional keyword arguments to pass to the ClientSession
transport
instance-attribute
url
instance-attribute
The URL of the Websocket endpoint to connect to.
_create_sse_session(*, url, headers=None, timeout=DEFAULT_HTTP_TIMEOUT, sse_read_timeout=DEFAULT_SSE_READ_TIMEOUT, session_kwargs=None, httpx_client_factory=None)
async
Create a new session to an MCP server using SSE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL of the SSE server |
required |
headers
|
dict[str, Any] | None
|
HTTP headers to send to the SSE endpoint |
None
|
timeout
|
float
|
HTTP timeout |
DEFAULT_HTTP_TIMEOUT
|
sse_read_timeout
|
float
|
SSE read timeout |
DEFAULT_SSE_READ_TIMEOUT
|
session_kwargs
|
dict[str, Any] | None
|
Additional keyword arguments to pass to the ClientSession |
None
|
httpx_client_factory
|
McpHttpClientFactory | None
|
Custom factory for httpx.AsyncClient (optional) |
None
|
Source code in griptape/tools/mcp/sessions.py
_create_stdio_session(*, command, args, env=None, cwd=None, encoding=DEFAULT_ENCODING, encoding_error_handler=DEFAULT_ENCODING_ERROR_HANDLER, session_kwargs=None)
async
Create a new session to an MCP server using stdio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Command to execute |
required |
args
|
list[str]
|
Arguments for the command |
required |
env
|
dict[str, str] | None
|
Environment variables for the command |
None
|
cwd
|
str | Path | None
|
Working directory for the command |
None
|
encoding
|
str
|
Character encoding |
DEFAULT_ENCODING
|
encoding_error_handler
|
Literal['strict', 'ignore', 'replace']
|
How to handle encoding errors |
DEFAULT_ENCODING_ERROR_HANDLER
|
session_kwargs
|
dict[str, Any] | None
|
Additional keyword arguments to pass to the ClientSession |
None
|
Source code in griptape/tools/mcp/sessions.py
_create_streamable_http_session(*, url, headers=None, timeout=DEFAULT_STREAMABLE_HTTP_TIMEOUT, sse_read_timeout=DEFAULT_STREAMABLE_HTTP_SSE_READ_TIMEOUT, terminate_on_close=True, session_kwargs=None, httpx_client_factory=None)
async
Create a new session to an MCP server using Streamable HTTP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL of the endpoint to connect to |
required |
headers
|
dict[str, Any] | None
|
HTTP headers to send to the endpoint |
None
|
timeout
|
timedelta
|
HTTP timeout |
DEFAULT_STREAMABLE_HTTP_TIMEOUT
|
sse_read_timeout
|
timedelta
|
How long (in seconds) the client will wait for a new event before disconnecting |
DEFAULT_STREAMABLE_HTTP_SSE_READ_TIMEOUT
|
terminate_on_close
|
bool
|
Whether to terminate the session on close |
True
|
session_kwargs
|
dict[str, Any] | None
|
Additional keyword arguments to pass to the ClientSession |
None
|
httpx_client_factory
|
McpHttpClientFactory | None
|
Custom factory for httpx.AsyncClient (optional) |
None
|
Source code in griptape/tools/mcp/sessions.py
_create_websocket_session(*, url, session_kwargs=None)
async
Create a new session to an MCP server using Websockets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL of the Websocket endpoint |
required |
session_kwargs
|
dict[str, Any] | None
|
Additional keyword arguments to pass to the ClientSession |
None
|
Source code in griptape/tools/mcp/sessions.py
create_session(connection)
async
Create a new session to an MCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
Connection
|
Connection config to use to connect to the server |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If transport is not recognized |
ValueError
|
If required parameters for the specified transport are missing |
Yields:
| Type | Description |
|---|---|
AsyncIterator[ClientSession]
|
A ClientSession |
Source code in griptape/tools/mcp/sessions.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |