sim-mcp
LLM工具、脚本和自动化的最小MCP兼容框架。
______________________________________________________________________
安装
npm install sim-mcp
npm install -D typescript tsx
npm install -g @modelcontextprotocol/inspector # (Optional) For MCP stdio testing______________________________________________________________________
用法
import { toolsJson, ChatAssistant, isMCP } from "sim-mcp";
/** Input for weather info for a city */
export interface WeatherInput {
/** Name of the city */
name: string;
}
/**
* Get weather info for a city.
* @param wi Weather input
* @returns Weather description string
*/
export async function Weather(wi: WeatherInput) {
return `The weather in ${wi.name} is sunny with 75°F.`;
}
/** Input for greeting */
export interface GreetInput {
/** Name of the person to greet */
name: string;
}
/**
* Return a friendly greeting for a user.
* @param gi Greeting input
* @returns Greeting string
*/
export function Greet(gi: GreetInput) {
return `Hello, ${gi.name}!`;
}
/** No input for random city generation */
export interface RandomCityInput {}
/**
* Generate a random city name using the LLM.
* @returns City name string
*/
export async function RandomCity(_: RandomCityInput) {
const bot = new ChatAssistant();
const res = await bot.solo("Suggest a random city name.");
return res.text || "Unknown City";
}
const allTools = toolsJson([Weather, Greet, RandomCity]);
const greetTool = toolsJson([Greet]);
if (!isMCP()) {
(async () => {
const bot = new ChatAssistant({
instructions: "Concise and helpful.",
tools: allTools,
});
// PROMPT: User-driven Q&A loop
let res;
do {
res = await bot.prompt("Ask about weather, greeting, or type 'exit':");
if (res.text) console.log("Assistant:", res.text);
} while (res.type !== "exit");
// CHAINING: Use RandomCity, then Weather
const city = await bot.decide("Pick a random city.");
if (city.error || !city.result) {
console.log("Error getting random city.");
return;
}
const weather = await bot.decide(`What's the weather in ${city.result}?`);
if (weather.error) {
console.log("Error getting weather.");
return;
}
console.log(`Random city: ${city.result}\nWeather: ${weather.text}`);
// DISCUSS: LLM-only chat (no tools)
const chatRes = await bot.discuss(
"Tell me something interesting about the world's capitals."
);
console.log("Discuss:", chatRes.text);
// DECIDE with custom tool set: only greet
const greetRes = await bot.decide("Say hello to Alex.", {
tools: greetTool,
});
console.log("Decide (greet only):", greetRes.text);
})();
}______________________________________________________________________
辅助方法
| 方法 | 用例 | 工具调用 | 用户输入 | 仅限LLM | 描述 |
|---|---|---|---|---|---|
prompt | 交互式问答/用户驱动聊天 | 是 | 是 | 有 | 用户键入问题,助手回答(有或没有工具) |
decide | 强制工具调用(仅函数调用) | 是 | 否 | 否 | LLM _必须_ 使用工具回答提示 |
chat | 通用(工具或文本,由LLM决定) | 可选 | 否 | 是 | LLM可以自由回答或调用工具(如果它决定) |
solo | 一次性,仅LLM响应(不使用工具) | 否 | 否 | 是 | 始终是一个LLM文本回复,没有函数调用 |
discuss | 聊天/讨论模式(不允许使用工具) | 否 | 否 | 是 | 无工具调用的对话、摘要或信息 |
- 所有方法都接受以下选项
{ model, instructions, tools }.
______________________________________________________________________
API
toolsJson(tools: Function[])ChatAssistant({ model?, instructions?, tools? })
- .prompt(message, options?) - .decide(message, options?) - .chat(message, options?) - .solo(message, options?) - .discuss(message, options?)
isMCP()
_默认导出_: { toolsJson, ChatAssistant, isMCP }
______________________________________________________________________
MCP工具测试
npx @modelcontextprotocol/inspector tsx yourfile.ts______________________________________________________________________
环境
- 使用
.env在项目根OPENAI_API_KEY以及其他变量。 .env是自动加载的。
______________________________________________________________________
关于
作者:丹·怀特黑德()\ 零配置,LLM和MCP的工具优先自动化。
______________________________________________________________________
许可证
麻省理工学院
