|
3 | 3 |
|
4 | 4 | import io.modelcontextprotocol.server.McpServerFeatures; |
5 | 5 | import jakarta.servlet.http.HttpSession; |
| 6 | +import org.apache.logging.log4j.Logger; |
6 | 7 | import org.jetbrains.annotations.NotNull; |
7 | 8 | import org.jspecify.annotations.NonNull; |
8 | 9 | import org.labkey.api.data.Container; |
9 | 10 | import org.labkey.api.security.User; |
10 | 11 | import org.labkey.api.services.ServiceRegistry; |
11 | 12 | import org.labkey.api.util.HtmlString; |
| 13 | +import org.labkey.api.util.logging.LogHelper; |
12 | 14 | import org.labkey.api.writer.ContainerUser; |
13 | 15 | import org.springframework.ai.chat.client.ChatClient; |
14 | 16 | import org.springframework.ai.chat.model.ToolContext; |
|
78 | 80 | /// |
79 | 81 | public interface McpService extends ToolCallbackProvider |
80 | 82 | { |
| 83 | + Logger LOG = LogHelper.getLogger(McpService.class, "MCP registration exceptions"); |
| 84 | + |
81 | 85 | // Interface for MCP classes that we will "ingest" using Spring annotations. Provides a few helper methods. |
82 | 86 | interface McpImpl |
83 | 87 | { |
@@ -119,13 +123,21 @@ static void setInstance(McpService service) |
119 | 123 |
|
120 | 124 | default void register(McpImpl mcp) |
121 | 125 | { |
122 | | - ToolCallback[] tools = ToolCallbacks.from(mcp); |
123 | | - if (tools.length > 0) |
124 | | - registerTools(Arrays.asList(tools), mcp); |
| 126 | + try |
| 127 | + { |
| 128 | + ToolCallback[] tools = ToolCallbacks.from(mcp); |
| 129 | + if (tools.length > 0) |
| 130 | + registerTools(Arrays.asList(tools), mcp); |
125 | 131 |
|
126 | | - var resources = new SyncMcpResourceProvider(List.of(mcp)).getResourceSpecifications(); |
127 | | - if (null != resources && !resources.isEmpty()) |
128 | | - registerResources(resources); |
| 132 | + var resources = new SyncMcpResourceProvider(List.of(mcp)).getResourceSpecifications(); |
| 133 | + if (null != resources && !resources.isEmpty()) |
| 134 | + registerResources(resources); |
| 135 | + } |
| 136 | + catch (Exception e) |
| 137 | + { |
| 138 | + String advice = e.getMessage().contains("victools") ? " Try doing a clean build of API." : ""; |
| 139 | + LOG.error("Exception while registering MCP implementations. The MCP server will not be available.{}", advice, e); |
| 140 | + } |
129 | 141 | } |
130 | 142 |
|
131 | 143 | void registerTools(@NotNull List<ToolCallback> tools, McpImpl mcp); |
|
0 commit comments