forked from modelcontextprotocol/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpSession.java
More file actions
80 lines (71 loc) · 2.67 KB
/
McpSession.java
File metadata and controls
80 lines (71 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright 2024-2024 the original author or authors.
*/
package io.modelcontextprotocol.spec;
import com.fasterxml.jackson.core.type.TypeReference;
import reactor.core.publisher.Mono;
/**
* Represents a Model Control Protocol (MCP) session that handles communication between
* clients and the server. This interface provides methods for sending requests and
* notifications, as well as managing the session lifecycle.
*
* <p>
* The session operates asynchronously using Project Reactor's {@link Mono} type for
* non-blocking operations. It supports both request-response patterns and one-way
* notifications.
* </p>
*
* @author Christian Tzolov
* @author Dariusz Jędrzejczyk
*/
public interface McpSession {
/**
* Sends a request to the model counterparty and expects a response of type T.
*
* <p>
* This method handles the request-response pattern where a response is expected from
* the client or server. The response type is determined by the provided
* TypeReference.
* </p>
* @param <T> the type of the expected response
* @param method the name of the method to be called on the counterparty
* @param requestParams the parameters to be sent with the request
* @param typeRef the TypeReference describing the expected response type
* @return a Mono that will emit the response when received
*/
<T> Mono<T> sendRequest(String method, Object requestParams, TypeReference<T> typeRef);
/**
* Sends a notification to the model client or server without parameters.
*
* <p>
* This method implements the notification pattern where no response is expected from
* the counterparty. It's useful for fire-and-forget scenarios.
* </p>
* @param method the name of the notification method to be called on the server
* @return a Mono that completes when the notification has been sent
*/
default Mono<Void> sendNotification(String method) {
return sendNotification(method, null);
}
/**
* Sends a notification to the model client or server with parameters.
*
* <p>
* Similar to {@link #sendNotification(String)} but allows sending additional
* parameters with the notification.
* </p>
* @param method the name of the notification method to be sent to the counterparty
* @param params parameters to be sent with the notification
* @return a Mono that completes when the notification has been sent
*/
Mono<Void> sendNotification(String method, Object params);
/**
* Closes the session and releases any associated resources asynchronously.
* @return a {@link Mono<Void>} that completes when the session has been closed.
*/
Mono<Void> closeGracefully();
/**
* Closes the session and releases any associated resources.
*/
void close();
}