-
Notifications
You must be signed in to change notification settings - Fork 8
MCP documentation proxy #7968
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
labkey-matthewb
wants to merge
5
commits into
develop
Choose a base branch
from
fb_documentation_proxy
base: develop
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
MCP documentation proxy #7968
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae69984
Proxy local calls to searchDocumentation() and retrieveDocument() to …
labkey-matthewb c31b4e0
Merge remote-tracking branch 'origin/26.7_fb_documentation_proxy' int…
labkey-matthewb b32381e
Merge remote-tracking branch 'origin/develop' into fb_documentation_p…
labkey-matthewb 163faa5
revert unrelated change
labkey-matthewb ccf7c45
comment
labkey-matthewb 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
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
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
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,51 @@ | ||
| /* | ||
| * Copyright (c) 2026 LabKey Corporation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.labkey.api.mcp; | ||
|
|
||
| import org.jetbrains.annotations.Nullable; | ||
| import org.labkey.api.services.ServiceRegistry; | ||
|
|
||
| /** | ||
| * Local implementation of documentation search/retrieval, backing the {@code searchDocumentation}/ | ||
| * {@code retrieveDocument} MCP tools in {@code CoreMcp}. Only registered on servers that host the LabKey | ||
| * documentation content and its vector store (currently www.labkey.org); every other server forwards those | ||
| * tool calls to www.labkey.org instead of calling this service. See {@link #isEnabled()}. | ||
| */ | ||
| public interface DocumentationService | ||
| { | ||
| static @Nullable DocumentationService get() | ||
| { | ||
| return ServiceRegistry.get().getService(DocumentationService.class); | ||
| } | ||
|
|
||
| static void setInstance(DocumentationService impl) | ||
| { | ||
| ServiceRegistry.get().registerService(DocumentationService.class, impl); | ||
| } | ||
|
|
||
| /** | ||
| * True if this server is enabled as the documentation source. The backing optional feature flag is owned by | ||
| * whichever module registers an implementation (currently serviceTools), not this interface, so the flag | ||
| * only exists at all on servers that have that module installed. | ||
| */ | ||
| boolean isEnabled(); | ||
|
|
||
| /** Returns a JSON string; see CoreMcp's searchDocumentation tool description for the response shape. */ | ||
| String searchDocumentation(String query, @Nullable Integer topK); | ||
|
|
||
| /** Returns a JSON string; see CoreMcp's retrieveDocument tool description for the response shape. */ | ||
| String retrieveDocument(String id); | ||
| } |
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,114 @@ | ||
| /* | ||
| * Copyright (c) 2026 LabKey Corporation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.labkey.api.mcp; | ||
|
|
||
| import io.modelcontextprotocol.client.McpClient; | ||
| import io.modelcontextprotocol.client.McpSyncClient; | ||
| import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport; | ||
| import io.modelcontextprotocol.spec.McpSchema; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.labkey.api.util.logging.LogHelper; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Proxy for calling an MCP tool on a remote server. | ||
| * | ||
| * A single {@link McpSyncClient} is created lazily and reused for the lifetime of this instance; the MCP | ||
| * initialize handshake only happens once, on the first forwarded call. | ||
| */ | ||
| public class McpToolProxy | ||
| { | ||
| private static final Logger LOG = LogHelper.getLogger(McpToolProxy.class, "MCP tool forwarding"); | ||
|
|
||
| private final String remoteBaseUrl; | ||
| private volatile McpSyncClient client; | ||
|
|
||
| public McpToolProxy(String remoteBaseUrl) | ||
| { | ||
| this.remoteBaseUrl = remoteBaseUrl; | ||
| } | ||
|
|
||
| private McpSyncClient getClient() | ||
| { | ||
| McpSyncClient c = client; | ||
| if (c == null) | ||
| { | ||
| synchronized (this) | ||
| { | ||
| c = client; | ||
| if (c == null) | ||
| { | ||
| var transport = HttpClientStreamableHttpTransport.builder(remoteBaseUrl).build(); | ||
| c = McpClient.sync(transport) | ||
| .clientInfo(McpSchema.Implementation.builder("labkey-server-forwarder", "1.0").build()) | ||
| .build(); | ||
| c.initialize(); | ||
| client = c; | ||
| } | ||
| } | ||
| } | ||
| return c; | ||
| } | ||
|
|
||
| // Drop the cached client after a failed call, so the next attempt reconnects instead of reusing a dead session | ||
| private synchronized void resetClient() | ||
| { | ||
| if (client != null) | ||
| { | ||
| try | ||
| { | ||
| client.closeGracefully(); | ||
| } | ||
| catch (RuntimeException ignore) | ||
| { | ||
| // already broken; nothing to do | ||
| } | ||
| client = null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calls {@code remoteToolName} on the remote MCP server with {@code arguments} and returns its text content | ||
| * (joined, if the tool returned more than one text content block). Throws if the remote server can't be | ||
| * reached or the remote tool itself reports an error. | ||
| */ | ||
| public String forward(String remoteToolName, Map<String, Object> arguments) | ||
| { | ||
| McpSchema.CallToolResult result; | ||
| try | ||
| { | ||
| result = getClient().callTool(McpSchema.CallToolRequest.builder(remoteToolName).arguments(arguments).build()); | ||
| } | ||
| catch (RuntimeException e) | ||
| { | ||
| LOG.error("Failed to forward MCP tool call '{}' to {}", remoteToolName, remoteBaseUrl, e); | ||
| resetClient(); | ||
| throw new McpException("Unable to reach " + remoteBaseUrl + " to forward '" + remoteToolName + "': " + e.getMessage()); | ||
| } | ||
|
|
||
| String text = result.content().stream() | ||
| .filter(content -> content instanceof McpSchema.TextContent) | ||
| .map(content -> ((McpSchema.TextContent) content).text()) | ||
| .collect(Collectors.joining("\n")); | ||
|
|
||
| if (Boolean.TRUE.equals(result.isError())) | ||
| throw new McpException("Remote tool '" + remoteToolName + "' at " + remoteBaseUrl + " reported an error: " + text); | ||
|
|
||
| return text; | ||
| } | ||
| } |
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add comment: "Don't allow proxying to yourself"