Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ header:
- 'pnpm-lock.yaml'
- '**/*.txt'
- '**/*.md'
- '**/*.prompt'

comment: on-failure

Expand Down Expand Up @@ -109,6 +110,20 @@ dependency:
license: EDL-1.0
- name: org.jline:jline
license: BSD-3-Clause
- name: com.github.victools:jsonschema-module-jackson
license: Apache-2.0
- name: com.fasterxml.jackson.module:jackson-module-jsonSchema
license: Apache-2.0
- name: io.swagger.core.v3:swagger-annotations
license: Apache-2.0
- name: com.github.victools:jsonschema-module-swagger-2
license: Apache-2.0
- name: org.antlr:antlr-runtime
license: BSD-3-Clause
- name: org.antlr:antlr4-runtime
license: BSD-3-Clause
- name: org.antlr:ST4
license: BSD-3-Clause
# Weak compatible licenses
- name: javax.servlet.jsp:jsp-api
license: CDDL-1.1
Expand Down
16 changes: 16 additions & 0 deletions bigtop-manager-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<description>Bigtop Manager Bom</description>

<properties>
<spring-ai.version>1.0.0-RC1</spring-ai.version>
<spring-boot.version>3.1.1</spring-boot.version>
<springdoc.version>2.2.0</springdoc.version>
<freemarker.version>2.3.32</freemarker.version>
Expand All @@ -55,6 +56,7 @@
<langchain4j.version>1.0.1-beta6</langchain4j.version>
<mybatis-spring-boot-starter.version>3.0.3</mybatis-spring-boot-starter.version>
<pagehelper-spring-boot-starter.version>2.1.0</pagehelper-spring-boot-starter.version>
<victools.version>4.29.0</victools.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -276,11 +278,25 @@
<artifactId>langchain4j-community-qianfan</artifactId>
<version>${langchain4j.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
<version>${spring-ai.version}</version>
</dependency>

<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jackson</artifactId>
<version>${victools.version}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope</artifactId>
<version>${langchain4j.version}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-reactor</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public static String getAgentCachePath() {
return getProjectStoreDir() + File.separator + "agent-caches";
}

public static String getPromptsPath() {
return getProjectResourcesDir() + File.separator + "prompts";
}

private static String getProjectResourcesDir() {
if (Environments.isDevMode()) {
return Objects.requireNonNull(ProjectPathUtils.class.getResource("/"))
Expand Down
10 changes: 9 additions & 1 deletion bigtop-manager-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai</artifactId>
</dependency>

<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -74,6 +77,11 @@
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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.apache.bigtop.manager.server.config;

import org.apache.bigtop.manager.server.holder.SpringContextHolder;
import org.apache.bigtop.manager.server.mcp.tool.McpTool;

import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class McpConfig {

@Bean
public ToolCallbackProvider mcpTools() {
return MethodToolCallbackProvider.builder()
.toolObjects(
(Object[]) SpringContextHolder.getMcpTools().values().toArray(new McpTool[0]))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.bigtop.manager.server.config;

import org.apache.bigtop.manager.server.interceptor.AuthInterceptor;
import org.apache.bigtop.manager.server.interceptor.MCPInterceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
Expand All @@ -35,6 +36,9 @@ public class WebConfig implements WebMvcConfigurer {
@Resource
private AuthInterceptor authInterceptor;

@Resource
private MCPInterceptor mcpInterceptor;

private static final String API_PREFIX = "/api";

private static final String PREFIXED_PACKAGE = "org.apache.bigtop.manager.server.controller";
Expand All @@ -45,10 +49,14 @@ public void addInterceptors(InterceptorRegistry registry) {
.addPathPatterns("/**")
// Server APIs
.excludePathPatterns("/api/salt", "/api/nonce", "/api/login")
// MCP APIs
.excludePathPatterns("/mcp/**")
// Frontend pages
.excludePathPatterns("/", "/ui/**", "/favicon.ico", "/error")
// Swagger pages
.excludePathPatterns("/swagger-ui/**", "/v3/**", "/swagger-ui.html");

registry.addInterceptor(mcpInterceptor).addPathPatterns("/mcp/**");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.bigtop.manager.server.command.factory.JobFactory;
import org.apache.bigtop.manager.server.command.validator.CommandValidator;
import org.apache.bigtop.manager.server.mcp.tool.McpTool;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand Down Expand Up @@ -52,4 +53,8 @@ public static Map<String, CommandValidator> getCommandValidators() {
public static Map<String, JobFactory> getJobFactories() {
return applicationContext.getBeansOfType(JobFactory.class);
}

public static Map<String, McpTool> getMcpTools() {
return applicationContext.getBeansOfType(McpTool.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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.apache.bigtop.manager.server.interceptor;

import org.apache.bigtop.manager.common.utils.JsonUtils;
import org.apache.bigtop.manager.server.utils.ResponseEntity;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Component
public class MCPInterceptor implements HandlerInterceptor {

private ResponseEntity<?> responseEntity;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (checkAuthenticated(request)) {
return HandlerInterceptor.super.preHandle(request, response, handler);
} else {
response.setHeader("Content-Type", "application/json; charset=UTF-8");
response.getWriter().write(JsonUtils.writeAsString(responseEntity));
return false;
}
}

@Override
public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}

private Boolean checkAuthenticated(HttpServletRequest request) {
return true;
}

private Boolean checkPermission() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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.apache.bigtop.manager.server.mcp.converter;

import org.apache.bigtop.manager.common.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.execution.ToolCallResultConverter;
import org.springframework.lang.Nullable;

import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Base64;
import java.util.Map;
import java.util.Objects;

/**
* Custom converter only replace JsonParser to JsonUtils.
* See original source code from {@link org.springframework.ai.tool.execution.DefaultToolCallResultConverter}
*/
public class JsonToolCallResultConverter implements ToolCallResultConverter {

private static final Logger logger = LoggerFactory.getLogger(JsonToolCallResultConverter.class);

@NotNull @Override
public String convert(@Nullable Object result, @Nullable Type returnType) {
if (returnType == Void.TYPE) {
logger.debug("The tool has no return type. Converting to conventional response.");
return JsonUtils.writeAsString("Done");
}
if (result instanceof RenderedImage) {
final var buf = new ByteArrayOutputStream(1024 * 4);
try {
ImageIO.write((RenderedImage) result, "PNG", buf);
} catch (IOException e) {
return "Failed to convert tool result to a base64 image: " + e.getMessage();
}
final var imgB64 = Base64.getEncoder().encodeToString(buf.toByteArray());
return JsonUtils.writeAsString(Map.of("mimeType", "image/png", "data", imgB64));
} else {
logger.debug("Converting tool result to JSON.");
return Objects.requireNonNull(JsonUtils.writeAsString(result));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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.apache.bigtop.manager.server.mcp.prompt;

import org.apache.bigtop.manager.common.utils.FileUtils;
import org.apache.bigtop.manager.common.utils.ProjectPathUtils;
import org.apache.bigtop.manager.server.exception.ServerException;

import java.io.File;

/**
* Static prompts for tools.
*/
public class Prompts {

public static final String SAMPLE = getText("sample.prompt");

private static String getText(String filename) {
String promptPath = ProjectPathUtils.getPromptsPath();
String filePath = promptPath + File.separator + filename;

try {
return FileUtils.readFile2Str(new File(filePath));
} catch (Exception e) {
throw new ServerException("Error reading prompt file: " + filePath, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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.apache.bigtop.manager.server.mcp.tool;

public interface McpTool {}
Loading
Loading