Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Support Model Context Protocol (MCP) https://github.com/siyuan-note/siyuan/issues/13795

Danielbb4eece1ec7d
diff --git a/kernel/mcp/client/convert.go b/kernel/mcp/client/convert.go
index 5e516a2dcb..64d3180ee0 100644
--- a/kernel/mcp/client/convert.go
+++ b/kernel/mcp/client/convert.go
@@ -21,7 +21,7 @@ import (
)
func convertMCPSchema(inputSchema any) tools.ToolSchema {
- schema := tools.ToolSchema{Type: "object"}
+ schema := tools.ToolSchema{}
if inputSchema == nil {
return schema
@@ -52,6 +52,29 @@ func convertMCPSchema(inputSchema any) tools.ToolSchema {
}
}
+ if oneOf, ok := m["oneOf"].([]any); ok {
+ schema.OneOf = convertSchemaArray(oneOf)
+ }
+
+ if anyOf, ok := m["anyOf"].([]any); ok {
+ schema.AnyOf = convertSchemaArray(anyOf)
+ }
+
+ if allOf, ok := m["allOf"].([]any); ok {
+ schema.AllOf = convertSchemaArray(allOf)
+ }
+
+ if ref, ok := m["$ref"].(string); ok {
+ schema.Ref = ref
+ }
+
+ if defs, ok := m["$defs"].(map[string]any); ok {
+ schema.Defs = make(map[string]tools.ToolSchema)
+ for name, def := range defs {
+ schema.Defs[name] = convertMCPSchema(def)
+ }
+ }
+
return schema
}
@@ -101,5 +124,37 @@ func convertMCPProperty(val any) tools.Property {
}
}
+ if oneOf, ok := m["oneOf"].([]any); ok {
+ prop.OneOf = convertPropArray(oneOf)
+ }
+
+ if anyOf, ok := m["anyOf"].([]any); ok {
+ prop.AnyOf = convertPropArray(anyOf)
+ }
+
+ if allOf, ok := m["allOf"].([]any); ok {
+ prop.AllOf = convertPropArray(allOf)
+ }
+
+ if ref, ok := m["$ref"].(string); ok {
+ prop.Ref = ref
+ }
+
return prop
}
+
+func convertSchemaArray(arr []any) []tools.ToolSchema {
+ result := make([]tools.ToolSchema, 0, len(arr))
+ for _, item := range arr {
+ result = append(result, convertMCPSchema(item))
+ }
+ return result
+}
+
+func convertPropArray(arr []any) []tools.Property {
+ result := make([]tools.Property, 0, len(arr))
+ for _, item := range arr {
+ result = append(result, convertMCPProperty(item))
+ }
+ return result
+}
diff --git a/kernel/mcp/handler.go b/kernel/mcp/handler.go
index 994493b84f..a5613d1931 100644
--- a/kernel/mcp/handler.go
+++ b/kernel/mcp/handler.go
@@ -24,9 +24,10 @@ import (
var supportedProtocolVersions = map[string]bool{
"2025-06-18": true,
"2024-11-05": true,
+ "2026-07-28": true,
}
-func processRequest(req *JsonRpcRequest, session *Session) any {
+func processRequest(req *JsonRpcRequest, session *Session, protocolVersion string) any {
isNotification := req.ID == nil
switch req.Method {
@@ -74,6 +75,50 @@ func processRequest(req *JsonRpcRequest, session *Session) any {
}
}
+func processRequest2026(req *JsonRpcRequest) any {
+ isNotification := req.ID == nil
+
+ switch req.Method {
+ case "server/discover":
+ if isNotification {
+ return nil
+ }
+ return handleDiscover(req)
+
+ case "ping":
+ if isNotification {
+ return nil
+ }
+ return &JsonRpcResponse{
+ JsonRpc: "2.0",
+ Result: map[string]interface{}{},
+ ID: req.ID,
+ }
+
+ case "tools/list":
+ if isNotification {
+ return nil
+ }
+ return handleToolsList2026(req.ID)
+
+ case "tools/call":
+ if isNotification {
+ return nil
+ }
+ return handleToolsCall(req)
+
+ default:
+ if isNotification {
+ return nil
+ }
+ return &JsonRpcErrorResponse{
+ JsonRpc: "2.0",
+ Error: RpcError{Code: -32601, Message: "Method not found"},
+ ID: req.ID,
+ }
+ }
+}
+
func handleInitialize(req *JsonRpcRequest, session *Session) *JsonRpcResponse {
params, _ := req.Params.(map[string]interface{})
@@ -168,3 +213,36 @@ func handleToolsCall(req *JsonRpcRequest) any {
ID: req.ID,
}
}
+
+func handleDiscover(req *JsonRpcRequest) *JsonRpcResponse {
+ return &JsonRpcResponse{
+ JsonRpc: "2.0",
+ Result: map[string]interface{}{
+ "protocolVersion": ProtocolV20260728,
+ "capabilities": ServerCapabilities{
+ Tools: &ToolsCapability{ListChanged: false},
+ },
+ "serverInfo": ServerInfo{
+ Name: ServerName,
+ Version: util.Ver,
+ },
+ },
+ ID: req.ID,
+ }
+}
+
+func handleToolsList2026(id any) *JsonRpcResponse {
+ toolList := make([]*tools.Tool, 0, len(tools.Registry))
+ for _, t := range tools.Registry {
+ toolList = append(toolList, t)
+ }
+ return &JsonRpcResponse{
+ JsonRpc: "2.0",
+ Result: map[string]interface{}{
+ "tools": toolList,
+ "ttlMs": 60000,
+ "cacheScope": "user",
+ },
+ ID: id,
+ }
+}
diff --git a/kernel/mcp/server.go b/kernel/mcp/server.go
index a7f94ea79d..0a133a4836 100644
--- a/kernel/mcp/server.go
+++ b/kernel/mcp/server.go
@@ -72,6 +72,12 @@ func handlePost(c *gin.Context) {
return
}
+ protoVersion := c.GetHeader("MCP-Protocol-Version")
+ if protoVersion == ProtocolV20260728 {
+ handlePost2026(c, &req)
+ return
+ }
+
var session *Session
if req.Method == "initialize" {
session = newSession()
@@ -88,12 +94,27 @@ func handlePost(c *gin.Context) {
}
}
- response := processRequest(&req, session)
+ response := processRequest(&req, session, protoVersion)
if req.Method == "initialize" {
c.Header("Mcp-Session-Id", session.ID)
}
+ writeResponse(c, response)
+}
+
+func handlePost2026(c *gin.Context, req *JsonRpcRequest) {
+ mcpMethod := c.GetHeader("Mcp-Method")
+ if mcpMethod != "" && mcpMethod != req.Method {
+ c.JSON(http.StatusBadRequest, gin.H{"error": "Mcp-Method header does not match body method"})
+ return
+ }
+
+ response := processRequest2026(req)
+ writeResponse(c, response)
+}
+
+func writeResponse(c *gin.Context, response any) {
switch v := response.(type) {
case nil:
c.Status(http.StatusAccepted)
diff --git a/kernel/mcp/tools/types.go b/kernel/mcp/tools/types.go
index 737f3c13a8..786d3cfc40 100644
--- a/kernel/mcp/tools/types.go
+++ b/kernel/mcp/tools/types.go
@@ -20,22 +20,32 @@ type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema ToolSchema `json:"inputSchema"`
+ OutputSchema *ToolSchema `json:"outputSchema,omitempty"`
Handler func(args map[string]interface{}) (CallToolResult, error) `json:"-"`
}
type ToolSchema struct {
- Type string `json:"type"`
- Properties map[string]Property `json:"properties"`
- Required []string `json:"required,omitempty"`
+ Type string `json:"type,omitempty"`
+ Properties map[string]Property `json:"properties,omitempty"`
+ Required []string `json:"required,omitempty"`
+ OneOf []ToolSchema `json:"oneOf,omitempty"`
+ AnyOf []ToolSchema `json:"anyOf,omitempty"`
+ AllOf []ToolSchema `json:"allOf,omitempty"`
+ Ref string `json:"$ref,omitempty"`
+ Defs map[string]ToolSchema `json:"$defs,omitempty"`
}
type Property struct {
- Type string `json:"type"`
+ Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Enum []string `json:"enum,omitempty"`
Items *Property `json:"items,omitempty"`
Properties map[string]Property `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
+ OneOf []Property `json:"oneOf,omitempty"`
+ AnyOf []Property `json:"anyOf,omitempty"`
+ AllOf []Property `json:"allOf,omitempty"`
+ Ref string `json:"$ref,omitempty"`
}
type CallToolResult struct {
diff --git a/kernel/mcp/types.go b/kernel/mcp/types.go
index 4754229aa6..32e3618591 100644
--- a/kernel/mcp/types.go
+++ b/kernel/mcp/types.go
@@ -17,10 +17,15 @@
package mcp
const (
- ProtocolVersion = "2025-06-18"
- ServerName = "SiYuan"
+ ProtocolVersion = "2025-06-18"
+ ProtocolV20260728 = "2026-07-28"
+ ServerName = "SiYuan"
)
+type Meta struct {
+ ClientInfo *ServerInfo `json:"io.modelcontextprotocol/clientInfo,omitempty"`
+}
+
type JsonRpcRequest struct {
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`