-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Load multiple sse servers on runtime #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rohitdutt-04
wants to merge
6
commits into
modelcontextprotocol:main
Choose a base branch
from
rohitdutt-04:load-servers-runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3edba37
Load multiple sse servers on runtime
rohitdutt-04 77b6cc9
fixed review comments
rohitdutt-04 5cd9bc0
removed headers from McpServerInstance
rohitdutt-04 42153c2
handled Stdio servers and made the server addition async
rohitdutt-04 273465d
updated file name
rohitdutt-04 67ee3b8
fixed build issue
rohitdutt-04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
mcp/src/main/java/io/modelcontextprotocol/client/McpServerInstance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package io.modelcontextprotocol.client; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| public class McpServerInstance { | ||
| private String name; | ||
| private String url; | ||
|
|
||
| private HashMap<String, String> headers; | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getUrl() { | ||
| return url; | ||
| } | ||
|
|
||
| public void setUrl(String url) { | ||
| this.url = url; | ||
| } | ||
|
|
||
| public HashMap<String, String> getHeaders() { | ||
| return headers; | ||
| } | ||
|
|
||
| public void setHeaders(HashMap<String, String> headers) { | ||
| this.headers = headers; | ||
| } | ||
|
|
||
| } | ||
42 changes: 42 additions & 0 deletions
42
mcp/src/main/java/io/modelcontextprotocol/client/transport/McpSseServerLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package io.modelcontextprotocol.client.transport; | ||
|
|
||
| import io.modelcontextprotocol.client.McpClient; | ||
| import io.modelcontextprotocol.client.McpSyncClient; | ||
| import java.util.*; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| import io.modelcontextprotocol.client.McpServerInstance; | ||
|
|
||
|
|
||
| public class McpSseServerLoader { | ||
|
|
||
| public List<McpSyncClient> initServers(List<McpServerInstance> servers) { | ||
|
|
||
| List<McpSyncClient> connectedClients = new CopyOnWriteArrayList<>(); | ||
|
|
||
| if (servers.isEmpty()) { | ||
| throw new IllegalArgumentException("No servers found to initialize."); | ||
| } | ||
|
|
||
| for (McpServerInstance server : servers) { | ||
|
|
||
| String serverUrl = server.getUrl(); | ||
| if (serverUrl == null || serverUrl.isEmpty()) { | ||
| System.out.println("Skipping " + server.getName() + ": URL not available"); | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| System.out.println("\nInitializing connection to: " + server.getName()); | ||
| HttpClientSseClientTransport newTransport = new HttpClientSseClientTransport(serverUrl); | ||
| McpSyncClient newClient = McpClient.sync(newTransport).build(); | ||
| newClient.initialize(); | ||
| connectedClients.add(newClient); | ||
| System.out.println("✅ Successfully connected to " + server.getName()); | ||
| } catch (Exception e) { | ||
| System.out.println("✗ Failed to connect to SSE server " + server.getName() + ": " + e.getMessage()); | ||
| } | ||
| } | ||
| return connectedClients; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.