Skip to content

Adopting with- style accessors as part of the Transport APIs #126

Open
@Joannis

Description

@Joannis

Is your feature request related to a problem? Please describe.
The current Transport APIs require the user to call connect() to establish a connection. From this point, the user has to manually track whether or not the connection is still alive, by handling any errors thrown by send or receive.

If a user wants to correctly handle error cases, this leads to more complexity in their implementations.

Describe the solution you'd like
In the Swift on Server ecosystem, we've adopted a with- style pattern, which compliments prior art in Swift's standard library. In there, we have a "bootstrap" for each transport that can be configured with the intended behavior of a transport connection.

let bootstrap = MCPTransportBootstrap()

A bootstrap connects in a with- style function call like so:

bootstrap.withClient { send, messages in
  try await send(SomeToolCall())

  for try await message in messages {
    // Handle incoming traffic
  }
}

Returning out of the withClient scope closes and cleans up the connection.
This pattern can be wrapped in more domain specific use cases like a request/response style flow in the higher level APIs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions