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

Daniel6de5ca5afb3b
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts
index f2a0a441d4..429484db0d 100644
--- a/app/src/layout/dock/AgentChat.ts
+++ b/app/src/layout/dock/AgentChat.ts
@@ -59,7 +59,7 @@ export class AgentChat extends Model {
const L = window.siyuan.languages;
panel.innerHTML = '<div class="agent-chat fn__flex-column fn__flex-1">' +
- '<div class="block__icons">' +
+ '<div class="block__icons fn__hidescrollbar">' +
'<div class="block__logo fn__flex-1 agent-chat__title">' + (L.agentChat || "Agent") + "</div>" +
'<span data-type="new-session" class="block__icon ariaLabel" data-position="north" aria-label="' + (L.agentNewSession || "New Session") + '">' +
'<svg><use xlink:href="#iconAdd"></use></svg>' +
@@ -77,7 +77,7 @@ export class AgentChat extends Model {
'<div class="agent-chat__input-area">' +
'<div class="agent-chat__composer-host"></div>' +
'<div class="agent-chat__buttons">' +
- '<span class="agent-chat__tokens" style="display:none"></span>' +
+ '<span class="agent-chat__tokens fn__none"></span>' +
'<button class="agent-chat__send b3-button b3-button--text">' + (L.agentSend || "Send") + "</button>" +
'<button class="agent-chat__stop b3-button b3-button--cancel fn__none">' + (L.agentStop || "Stop") + "</button>" +
"</div>" +
@@ -422,7 +422,7 @@ export class AgentChat extends Model {
this.sessionTotalDuration = 0;
this.currentToolCalls = [];
if (this.tokenDisplayEl) {
- this.tokenDisplayEl.style.display = "none";
+ this.tokenDisplayEl.classList.add("fn__none");
}
this.messagesContainer.innerHTML = "";
this.titleElement.textContent = this.defaultTitle;
@@ -851,7 +851,7 @@ export class AgentChat extends Model {
text += " \u00B7 " + (minutes > 0 ? minutes + "m" : "") + seconds + "s";
}
this.tokenDisplayEl.textContent = text;
- this.tokenDisplayEl.style.display = "";
+ this.tokenDisplayEl.classList.remove("fn__none");
}
private appendUsage(promptTokens: number, completionTokens: number) {
@@ -872,8 +872,7 @@ export class AgentChat extends Model {
this.sendBtn.classList.toggle("fn__none", streaming);
this.stopBtn.classList.toggle("fn__none", !streaming);
if (this.composerHost) {
- this.composerHost.style.pointerEvents = streaming ? "none" : "";
- this.composerHost.style.opacity = streaming ? "0.5" : "";
+ this.composerHost.classList.toggle("agent-chat__composer-host--disabled", streaming);
}
}
diff --git a/kernel/mcp/tools/web_fetch.go b/kernel/mcp/tools/web_fetch.go
new file mode 100644
index 0000000000..6f6c5cbd8b
--- /dev/null
+++ b/kernel/mcp/tools/web_fetch.go
@@ -0,0 +1,59 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package tools
+
+import (
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+var WebFetchTool = &Tool{
+ Name: "web_fetch",
+ Description: "Fetch the content of a web page and convert it to Markdown or plain text.\n- url: The web page URL to fetch (must start with http:// or https://).\n- format: Output format, 'markdown' (default) or 'text'.",
+ InputSchema: ToolSchema{
+ Type: "object",
+ Properties: map[string]Property{
+ "url": {Type: "string", Description: "The page URL to fetch (must start with http:// or https://)"},
+ "format": {Type: "string", Description: "Output format: 'markdown' or 'text' (default 'markdown')", Enum: []string{"markdown", "text"}},
+ },
+ Required: []string{"url"},
+ },
+ Handler: webFetchHandler,
+}
+
+func init() {
+ register(WebFetchTool)
+}
+
+func webFetchHandler(args map[string]interface{}) (CallToolResult, error) {
+ rawURL, _ := args["url"].(string)
+ format, _ := args["format"].(string)
+ if format == "" {
+ format = "markdown"
+ }
+
+ result, err := util.WebFetch(rawURL, format)
+ if err != nil {
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: "web_fetch error: " + err.Error()}},
+ IsError: true,
+ }, nil
+ }
+
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: result}},
+ }, nil
+}
diff --git a/kernel/util/webfetch.go b/kernel/util/webfetch.go
new file mode 100644
index 0000000000..be3ddd2ead
--- /dev/null
+++ b/kernel/util/webfetch.go
@@ -0,0 +1,104 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package util
+
+import (
+ "errors"
+ "fmt"
+ "io"
+ "net"
+ "net/url"
+ "strings"
+
+ "github.com/siyuan-note/httpclient"
+)
+
+const (
+ maxWebFetchBytes = 5 * 1024 * 1024 // 5MB
+ maxWebFetchChars = 50000
+)
+
+func WebFetch(rawURL, format string) (string, error) {
+ u, err := url.Parse(rawURL)
+ if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
+ return "", errors.New("URL must start with http:// or https://")
+ }
+ if u.Host == "" {
+ return "", errors.New("URL has no host")
+ }
+
+ host := u.Hostname()
+ ips, err := net.LookupIP(host)
+ if err != nil {
+ return "", errors.New("failed to resolve host: " + err.Error())
+ }
+ for _, ip := range ips {
+ if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsPrivate() || ip.IsUnspecified() {
+ return "", errors.New("access to private/internal IP is prohibited")
+ }
+ }
+
+ resp, err := httpclient.NewBrowserRequest().Get(rawURL)
+ if err != nil {
+ return "", errors.New("fetch failed: " + err.Error())
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode >= 400 {
+ return "", fmt.Errorf("HTTP %d", resp.StatusCode)
+ }
+
+ if resp.ContentLength > maxWebFetchBytes {
+ return "", errors.New("response too large (exceeds 5MB limit)")
+ }
+
+ body, err := io.ReadAll(io.LimitReader(resp.Body, maxWebFetchBytes))
+ if err != nil {
+ return "", errors.New("read body failed: " + err.Error())
+ }
+
+ contentType := resp.Header.Get("Content-Type")
+ htmlStr := string(body)
+
+ isHTML := strings.HasPrefix(contentType, "text/html")
+ if !isHTML {
+ return truncateRunes(htmlStr, maxWebFetchChars), nil
+ }
+
+ engine := NewLute()
+ var result string
+ switch format {
+ case "text":
+ result = engine.HTML2Text(htmlStr)
+ default: // markdown
+ md, mdErr := engine.HTML2Markdown(htmlStr)
+ if mdErr != nil {
+ return "", errors.New("HTML to Markdown conversion failed: " + mdErr.Error())
+ }
+ result = md
+ }
+
+ return truncateRunes(result, maxWebFetchChars), nil
+}
+
+func truncateRunes(s string, maxChars int) string {
+ runes := []rune(s)
+ if len(runes) <= maxChars {
+ return s
+ }
+ return string(runes[:maxChars]) + "\n\n...content truncated, total length " + fmt.Sprintf("%d", len(runes)) + " characters..."
+}