public interface McpTransport
The McpTransport interface provides the foundation for implementing custom transport mechanisms in the Model Context Protocol. It handles the bidirectional communication between the client and server components, supporting asynchronous message exchange using JSON-RPC format.
Implementations of this interface are responsible for:
The transport layer is designed to be protocol-agnostic, allowing for various implementations such as WebSocket, HTTP, or custom protocols.
限定符和类型 | 方法和说明 |
---|---|
default void |
close()
Closes the transport connection and releases any associated resources.
|
reactor.core.publisher.Mono<java.lang.Void> |
closeGracefully()
Closes the transport connection and releases any associated resources
asynchronously.
|
default reactor.core.publisher.Mono<java.lang.Void> |
connect(java.util.function.Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>,reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler)
已过时。
This is only relevant for client-side transports and will be removed
from this interface in 0.9.0.
|
reactor.core.publisher.Mono<java.lang.Void> |
sendMessage(McpSchema.JSONRPCMessage message)
Sends a message to the peer asynchronously.
|
<T> T |
unmarshalFrom(java.lang.Object data,
com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
Unmarshals the given data into an object of the specified type.
|
@Deprecated default reactor.core.publisher.Mono<java.lang.Void> connect(java.util.function.Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>,reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler)
This method should be called before any message exchange can occur. It sets up the necessary resources and establishes the connection to the server.
default void close()
This method ensures proper cleanup of resources when the transport is no longer needed. It should handle the graceful shutdown of any active connections.
reactor.core.publisher.Mono<java.lang.Void> closeGracefully()
Mono
that completes when the connection has been closed.reactor.core.publisher.Mono<java.lang.Void> sendMessage(McpSchema.JSONRPCMessage message)
This method handles the transmission of messages to the server in an asynchronous manner. Messages are sent in JSON-RPC format as specified by the MCP protocol.
message
- the McpSchema.JSONRPCMessage
to be sent to the serverMono
that completes when the message has been sent<T> T unmarshalFrom(java.lang.Object data, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
T
- the type of the object to unmarshaldata
- the data to unmarshaltypeRef
- the type reference for the object to unmarshal