Skip to content

Commit fe4fffa

Browse files
author
linyuan.yang
committed
进一步移除 langchain 的 mcp 代码
1 parent acf0f71 commit fe4fffa

68 files changed

Lines changed: 290 additions & 201 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/backend/command/scorpio.command/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "rimraf dist && tsc -b --force"
99
},
1010
"dependencies": {
11-
"@langchain/core": "catalog:",
11+
"scorpio.llm": "workspace:*",
1212
"zod": "catalog:"
1313
},
1414
"devDependencies": {

‎packages/backend/command/scorpio.command/src/codeTool.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import os from 'os';
33
import path from 'path';
44
import crypto from 'crypto';
55
import { z } from 'zod';
6-
import { DynamicStructuredTool, type StructuredToolInterface } from '@langchain/core/tools';
6+
import { createAgentTool, type AgentTool } from "scorpio.llm";
77
import { formatCommandError } from './errors';
88
import { createErrorResult, type CommandToolResult } from './result';
99
import { runProgram, runShellCommand } from './runtime/foreground';
@@ -157,8 +157,8 @@ async function executeCode(input: CodeToolInput, opts: ExecuteCodeOptions): Prom
157157
}
158158
}
159159

160-
export function createShellTool({ name = 'shell', description, processManager = defaultProcessManager }: ShellToolOptions): StructuredToolInterface {
161-
return new DynamicStructuredTool({
160+
export function createShellTool({ name = 'shell', description, processManager = defaultProcessManager }: ShellToolOptions): AgentTool {
161+
return createAgentTool({
162162
name,
163163
description,
164164
schema: shellToolSchema as any,
@@ -170,9 +170,9 @@ export function createShellTool({ name = 'shell', description, processManager =
170170
});
171171
}
172172

173-
export function createScriptCodeTool({ name, description, runtime, interpreter, preArgs = [], ext, processManager = defaultProcessManager }: ScriptCodeToolOptions): StructuredToolInterface | null {
173+
export function createScriptCodeTool({ name, description, runtime, interpreter, preArgs = [], ext, processManager = defaultProcessManager }: ScriptCodeToolOptions): AgentTool | null {
174174
if (!isCommandAvailable(interpreter)) return null;
175-
return new DynamicStructuredTool({
175+
return createAgentTool({
176176
name,
177177
description,
178178
schema: scriptCodeSchema as any,
@@ -187,8 +187,8 @@ export function createScriptCodeTool({ name, description, runtime, interpreter,
187187
});
188188
}
189189

190-
export function createReadProcessTool({ name = 'read_process', description, processManager = defaultProcessManager }: ReadProcessToolOptions): StructuredToolInterface {
191-
return new DynamicStructuredTool({
190+
export function createReadProcessTool({ name = 'read_process', description, processManager = defaultProcessManager }: ReadProcessToolOptions): AgentTool {
191+
return createAgentTool({
192192
name,
193193
description,
194194
schema: readProcessToolSchema as any,
@@ -199,8 +199,8 @@ export function createReadProcessTool({ name = 'read_process', description, proc
199199
});
200200
}
201201

202-
export function createWriteProcessTool({ name = 'write_process', description, processManager = defaultProcessManager }: WriteProcessToolOptions): StructuredToolInterface {
203-
return new DynamicStructuredTool({
202+
export function createWriteProcessTool({ name = 'write_process', description, processManager = defaultProcessManager }: WriteProcessToolOptions): AgentTool {
203+
return createAgentTool({
204204
name,
205205
description,
206206
schema: writeProcessToolSchema as any,

‎packages/backend/llm/scorpio.llm/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./messages";
22
export * from "./model";
3+
export * from "./tools";
34
export * from "./capabilities";
45
export * from "./embedding";
56
export * from "./registry";

‎packages/backend/llm/scorpio.llm/src/model.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
22
import type { BaseMessage } from "@langchain/core/messages";
33
import { AIMessageChunk } from "@langchain/core/messages";
4-
import type { StructuredToolInterface } from "@langchain/core/tools";
4+
import { toOpenAIToolFormat, type AgentTool } from "./tools";
55
import type { ChatMessage } from "./messages";
66
import { toBaseMessages, toChatMessage } from "./messageConverter";
77
import { type LLMInfo, getLLMInfo } from "./capabilities";
@@ -44,7 +44,7 @@ export interface StructuredInvokeOptions extends ModelInvokeOptions {
4444
export interface IModelService {
4545
readonly config: ModelConfig;
4646
invoke(prompt: string | ChatMessage[], options?: ModelInvokeOptions): Promise<ChatMessage>;
47-
bindTools(tools: any[]): void;
47+
bindTools(tools: AgentTool[]): void;
4848
invokeStructured<T = any>(schema: any, prompt: string | ChatMessage[], options?: StructuredInvokeOptions): Promise<T>;
4949
stream(messages: string | ChatMessage[], options?: ModelInvokeOptions): Promise<AsyncIterable<ChatMessage>>;
5050
/**
@@ -116,10 +116,11 @@ export abstract class ModelServiceBase<TModel extends BaseChatModel = BaseChatMo
116116
return toChatMessage(result);
117117
}
118118

119-
bindTools(tools: StructuredToolInterface[]): void {
119+
bindTools(tools: AgentTool[]): void {
120120
if (!this.model) throw new Error(`${this.constructor.name} is not initialized`);
121121
if (!this.model.bindTools) throw new Error(`${this.constructor.name} does not support tools`);
122-
this.boundModel = this.model.bindTools(tools);
122+
// LangChain 模型只认识自家工具或 OpenAI 格式,轻量工具先转换再绑定
123+
this.boundModel = this.model.bindTools(tools.map(toOpenAIToolFormat));
123124
}
124125

125126
abstract invokeStructured<T = any>(schema: any, prompt: string | ChatMessage[], options?: StructuredInvokeOptions): Promise<T>;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { z } from "zod";
2+
3+
/**
4+
* 轻量结构化工具接口 —— 全项目的工具边界。
5+
*
6+
* 替代 @langchain/core 的 StructuredToolInterface:工具生产方(内置工具、MCP 包装、
7+
* plugins)与消费方(各 LLM provider 的 bindTools、Agent 执行链路)只依赖这四个成员。
8+
* schema 统一为 JSON Schema 纯对象(zod 由 createAgentTool 自动转换)。
9+
*/
10+
export interface AgentTool {
11+
readonly name: string;
12+
readonly description?: string;
13+
/** 工具入参的 JSON Schema */
14+
readonly schema: Record<string, any>;
15+
invoke(input: Record<string, any>, config?: ToolInvokeConfig): Promise<any>;
16+
}
17+
18+
/**
19+
* 工具执行时框架注入的上下文。
20+
* 原先塞在 LangChain RunnableConfig.configurable 里的 think 预留约定,现在有真实类型。
21+
*/
22+
export interface ToolInvokeConfig {
23+
/** 取消信号 */
24+
signal?: AbortSignal;
25+
/** think 流预留 id:框架执行工具前生成,工具派发子思考流时与之关联 */
26+
thinkId?: string;
27+
/** 工具启动子思考流时声明一次(见 SingleAgentService 的 think 预留逻辑) */
28+
onCreateThink?: (taskId: string) => Promise<void>;
29+
}
30+
31+
/** createAgentTool 的定义参数 */
32+
export interface ToolDefinition {
33+
name: string;
34+
description?: string;
35+
/** zod schema 或 JSON Schema 纯对象 */
36+
schema: z.ZodType | Record<string, any>;
37+
func: (input: any, config?: ToolInvokeConfig) => Promise<any>;
38+
}
39+
40+
/**
41+
* 创建结构化工具 —— 对应原先的 new DynamicStructuredTool({...})。
42+
*
43+
* zod schema 会在构造时转换为 JSON Schema 暴露给模型,并在 invoke 时对入参
44+
* 做 parse 校验(默认值 / 类型收敛 / 未知字段剥离),与 DynamicStructuredTool 行为一致;
45+
* parse 失败抛 ZodError,由 Agent 执行链路统一转为 error ToolMessage。
46+
*/
47+
export function createAgentTool(def: ToolDefinition): AgentTool {
48+
const zodSchema = isZodSchema(def.schema) ? def.schema : undefined;
49+
// io:"input" 视角:带默认值的字段不再出现在 required 里(模型侧可省略,
50+
// invoke 时由 zod parse 补默认值)
51+
const jsonSchema = zodSchema ? z.toJSONSchema(zodSchema, { io: "input" }) : def.schema as Record<string, any>;
52+
return {
53+
name: def.name,
54+
description: def.description,
55+
schema: jsonSchema,
56+
invoke: async (input, config) => {
57+
const parsed = zodSchema ? zodSchema.parse(input) : input;
58+
return def.func(parsed, config);
59+
},
60+
};
61+
}
62+
63+
function isZodSchema(schema: unknown): schema is z.ZodType {
64+
return !!schema && typeof schema === "object" && typeof (schema as z.ZodType).parse === "function";
65+
}
66+
67+
/**
68+
* 将 Zod schema 统一转换为 JSON Schema 纯对象;已是普通对象(JSON Schema)时原样返回。
69+
* 供各 provider 在结构化输出(invokeStructured)时使用。
70+
*/
71+
export function toJsonSchema(schema: any): any {
72+
if (isZodSchema(schema)) {
73+
return z.toJSONSchema(schema);
74+
}
75+
return schema;
76+
}
77+
78+
/**
79+
* 转换为 OpenAI 工具调用格式。
80+
* LangChain 各 chat 模型(ChatOllama / ChatGoogleGenerativeAI 等)的 bindTools 对
81+
* 非 LangChain 工具会按格式识别透传:OpenAI 格式(isOpenAITool)可被正确转换。
82+
*/
83+
export function toOpenAIToolFormat(tool: AgentTool): {
84+
type: "function";
85+
function: { name: string; description?: string; parameters: Record<string, any> };
86+
} {
87+
return {
88+
type: "function",
89+
function: {
90+
name: tool.name,
91+
description: tool.description,
92+
parameters: tool.schema,
93+
},
94+
};
95+
}

‎packages/backend/plugins/agent/agent.agenda/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"build": "rimraf dist && tsc -b --force && node scripts/copy-assets.cjs"
99
},
1010
"dependencies": {
11-
"@langchain/core": "catalog:",
1211
"better-sqlite3": "catalog:",
1312
"scorpio.ai": "workspace:*",
1413
"scorpio.di": "workspace:*",

‎packages/backend/plugins/agent/agent.agenda/src/Tools/AgendaToolProvider.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DynamicStructuredTool } from "@langchain/core/tools";
1+
import { createAgentTool, type AgentTool } from "scorpio.ai";
22
import { z } from "zod";
33
import {
44
AgendaAssignee,
@@ -70,10 +70,10 @@ export class AgendaToolProvider {
7070
* channelSessionId 由调用方注入,会写到新建 trigger 的 channelSessionId。
7171
* admin 路径调用(背景任务、route handler)传 0。
7272
*/
73-
static getTools(agendaService: IAgendaService, channelSessionId: number): DynamicStructuredTool[] {
73+
static getTools(agendaService: IAgendaService, channelSessionId: number): AgentTool[] {
7474
const descs = agendaService.getToolDescs();
7575
return [
76-
new DynamicStructuredTool({
76+
createAgentTool({
7777
name: AGENDA_CREATE_TOOL_NAME,
7878
description: descs.create,
7979
schema: z.object({
@@ -111,7 +111,7 @@ export class AgendaToolProvider {
111111
}
112112
},
113113
}),
114-
new DynamicStructuredTool({
114+
createAgentTool({
115115
name: AGENDA_LIST_TOOL_NAME,
116116
description: descs.list,
117117
schema: z.object({
@@ -131,7 +131,7 @@ export class AgendaToolProvider {
131131
}
132132
},
133133
}),
134-
new DynamicStructuredTool({
134+
createAgentTool({
135135
name: AGENDA_GET_TOOL_NAME,
136136
description: descs.get,
137137
schema: z.object({
@@ -149,7 +149,7 @@ export class AgendaToolProvider {
149149
}
150150
},
151151
}),
152-
new DynamicStructuredTool({
152+
createAgentTool({
153153
name: AGENDA_EDIT_TOOL_NAME,
154154
description: descs.edit,
155155
schema: z.object({
@@ -181,7 +181,7 @@ export class AgendaToolProvider {
181181
}
182182
},
183183
}),
184-
new DynamicStructuredTool({
184+
createAgentTool({
185185
name: AGENDA_CLOSE_TOOL_NAME,
186186
description: descs.close,
187187
schema: z.object({
@@ -204,7 +204,7 @@ export class AgendaToolProvider {
204204
}
205205
},
206206
}),
207-
new DynamicStructuredTool({
207+
createAgentTool({
208208
name: AGENDA_WIKI_TOOL_NAME,
209209
description: AGENDA_WIKI_TOOL_DESCRIPTION,
210210
schema: z.object({}),

‎packages/backend/plugins/agent/agent.memory/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"build": "rimraf dist && tsc -b --force && node scripts/copy-assets.cjs"
99
},
1010
"dependencies": {
11-
"@langchain/core": "catalog:",
1211
"better-sqlite3": "catalog:",
1312
"scorpio.ai": "workspace:*",
1413
"scorpio.di": "workspace:*",

‎packages/backend/plugins/agent/agent.memory/src/Tools/MemoryToolProvider.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { DynamicStructuredTool } from "@langchain/core/tools";
2+
import { createAgentTool, type AgentTool } from "scorpio.ai";
33
import type { IMemoryService } from "../Service/IMemoryService";
44
import { MemoryScope } from "../Storage/IMemoryStore";
55

@@ -20,7 +20,7 @@ const REMEMBER_MEMORY_TOOL_DESCRIPTION = 'Queue a memory only when the user expl
2020
* - search_memory 0 命中时返回明确文本(避免 agent 误以为没记忆系统)
2121
*/
2222
export class MemoryToolProvider {
23-
static getTools(service: IMemoryService): DynamicStructuredTool[] {
23+
static getTools(service: IMemoryService): AgentTool[] {
2424
const descs = service.getToolDescs();
2525
return [
2626
MemoryToolProvider.createRememberTool(service, REMEMBER_MEMORY_TOOL_DESCRIPTION),
@@ -32,8 +32,8 @@ export class MemoryToolProvider {
3232
private static createRememberTool(
3333
service: IMemoryService,
3434
description: string,
35-
): DynamicStructuredTool {
36-
return new DynamicStructuredTool({
35+
): AgentTool {
36+
return createAgentTool({
3737
name: REMEMBER_MEMORY_TOOL_NAME,
3838
description,
3939
schema: z.object({
@@ -49,8 +49,8 @@ export class MemoryToolProvider {
4949
});
5050
}
5151

52-
private static createReadTool(service: IMemoryService, description: string): DynamicStructuredTool {
53-
return new DynamicStructuredTool({
52+
private static createReadTool(service: IMemoryService, description: string): AgentTool {
53+
return createAgentTool({
5454
name: READ_MEMORY_TOOL_NAME,
5555
description,
5656
schema: z.object({
@@ -73,8 +73,8 @@ export class MemoryToolProvider {
7373
});
7474
}
7575

76-
private static createSearchTool(service: IMemoryService, description: string): DynamicStructuredTool {
77-
return new DynamicStructuredTool({
76+
private static createSearchTool(service: IMemoryService, description: string): AgentTool {
77+
return createAgentTool({
7878
name: SEARCH_MEMORY_TOOL_NAME,
7979
description,
8080
schema: z.object({

‎packages/backend/plugins/agent/agent.note/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"build": "rimraf dist && tsc -b --force && node scripts/copy-assets.cjs"
99
},
1010
"dependencies": {
11-
"@langchain/core": "catalog:",
1211
"@langchain/textsplitters": "^1.0.1",
1312
"better-sqlite3": "catalog:",
1413
"scorpio.ai": "workspace:*",

0 commit comments

Comments
 (0)