From 3f565c45bb67d104e9793b90e64bba3d2b1fa0cd Mon Sep 17 00:00:00 2001
From: HawickMason <1914991129@qq.com>
Date: Tue, 21 Jan 2025 19:15:57 +0800
Subject: [PATCH 1/4] feat(mcp_memory): add mcp server for memory
---
jcommon/mcp/mcp-memory/pom.xml | 57 +++
.../mone/mcp/memory/MemoryMcpBootstrap.java | 13 +
.../config/McpStdioTransportConfig.java | 21 +
.../mcp/memory/function/MemoryFunctions.java | 417 ++++++++++++++++++
.../run/mone/mcp/memory/graph/Entity.java | 22 +
.../mone/mcp/memory/graph/KnowledgeGraph.java | 11 +
.../memory/graph/KnowledgeGraphManager.java | 232 ++++++++++
.../run/mone/mcp/memory/graph/Relation.java | 17 +
.../mcp/memory/server/MemoryMcpServer.java | 103 +++++
.../src/main/resources/application.properties | 2 +
.../mcp/memory/test/MemoryFunctionsTest.java | 180 ++++++++
.../resources/application-test.properties | 6 +
jcommon/mcp/pom.xml | 3 +-
13 files changed, 1083 insertions(+), 1 deletion(-)
create mode 100644 jcommon/mcp/mcp-memory/pom.xml
create mode 100644 jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/MemoryMcpBootstrap.java
create mode 100644 jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/config/McpStdioTransportConfig.java
create mode 100644 jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/function/MemoryFunctions.java
create mode 100644 jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/graph/Entity.java
create mode 100644 jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/graph/KnowledgeGraph.java
create mode 100644 jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/graph/KnowledgeGraphManager.java
create mode 100644 jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/graph/Relation.java
create mode 100644 jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/server/MemoryMcpServer.java
create mode 100644 jcommon/mcp/mcp-memory/src/main/resources/application.properties
create mode 100644 jcommon/mcp/mcp-memory/src/test/java/run/mone/mcp/memory/test/MemoryFunctionsTest.java
create mode 100644 jcommon/mcp/mcp-memory/src/test/resources/application-test.properties
diff --git a/jcommon/mcp/mcp-memory/pom.xml b/jcommon/mcp/mcp-memory/pom.xml
new file mode 100644
index 000000000..9234e221b
--- /dev/null
+++ b/jcommon/mcp/mcp-memory/pom.xml
@@ -0,0 +1,57 @@
+
+
+ 4.0.0
+
+ run.mone
+ mcp
+ 1.6.1-jdk21-SNAPSHOT
+
+
+ mcp-memory
+
+
+ 17
+ 17
+
+
+
+
+
+
+ maven-compiler-plugin
+ 3.11.0
+
+ ${maven.compiler.source}
+ ${maven.compiler.target}
+ true
+ UTF-8
+
+ ${project.basedir}/src/main/java
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+ 2.7.14
+
+ run.mone.mcp.memory.MemoryMcpBootstrap
+ app
+
+
+
+
+ repackage
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/MemoryMcpBootstrap.java b/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/MemoryMcpBootstrap.java
new file mode 100644
index 000000000..0f4c938ec
--- /dev/null
+++ b/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/MemoryMcpBootstrap.java
@@ -0,0 +1,13 @@
+package run.mone.mcp.memory;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
+
+@SpringBootApplication
+@ComponentScan("run.mone.mcp.memory")
+public class MemoryMcpBootstrap {
+ public static void main(String[] args) {
+ SpringApplication.run(MemoryMcpBootstrap.class, args);
+ }
+}
\ No newline at end of file
diff --git a/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/config/McpStdioTransportConfig.java b/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/config/McpStdioTransportConfig.java
new file mode 100644
index 000000000..ec3e5ba9f
--- /dev/null
+++ b/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/config/McpStdioTransportConfig.java
@@ -0,0 +1,21 @@
+package run.mone.mcp.memory.config;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import run.mone.hive.mcp.server.transport.StdioServerTransport;
+
+@Configuration
+@ConditionalOnProperty(name = "stdio.enabled", havingValue = "true")
+class McpStdioTransportConfig {
+ /**
+ * stdio 通信
+ * @param mapper
+ * @return
+ */
+ @Bean
+ StdioServerTransport stdioServerTransport(ObjectMapper mapper) {
+ return new StdioServerTransport(mapper);
+ }
+}
diff --git a/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/function/MemoryFunctions.java b/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/function/MemoryFunctions.java
new file mode 100644
index 000000000..13aaca558
--- /dev/null
+++ b/jcommon/mcp/mcp-memory/src/main/java/run/mone/mcp/memory/function/MemoryFunctions.java
@@ -0,0 +1,417 @@
+package run.mone.mcp.memory.function;
+
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import run.mone.hive.mcp.spec.McpSchema;
+import run.mone.hive.mcp.spec.McpSchema.CallToolResult;
+import run.mone.mcp.memory.graph.KnowledgeGraphManager;
+import run.mone.mcp.memory.graph.Entity;
+import run.mone.mcp.memory.graph.KnowledgeGraph;
+import run.mone.mcp.memory.graph.Relation;
+
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@Data
+@Slf4j
+public class MemoryFunctions {
+
+ private static final KnowledgeGraphManager graphManager = new KnowledgeGraphManager();
+
+ private static final ObjectMapper objectMapper = new ObjectMapper();
+
+ @Data
+ public static class CreateEntitiesFunction implements Function