Token导航 LogoToken导航TokenDH.com
toolcall (Sceiler) logo
AI代理stdio官方级别未说明来源级核验

toolcall (Sceiler)

MCP Server

tsx

一个零样板代码创建MCP(Model Context Protocol)服务器的工具,支持TypeScript类型安全和多种传输协议。

工具数

4

提示词数

0

GitHub Stars

1

资源数

0
服务器开发API集成TypeScriptClaudeJavaScriptClaude

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

sceiler

提供方

sceiler

最后核验

2026/5/17 20:21

运行时

Node.js

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

命令预览

npx tsx server.ts

详细介绍

工具调用

使用零样板创建MCP(模型上下文协议)服务器。

](https://www.npmjs.com/package/toolcall) ![License: MIT](https://opensource.org/licenses/MIT)

特性

  • 最小API -只有两个功能: serve()tool()
  • 类型安全 -通过Zod模式验证完全支持TypeScript
  • 多个传输 -支持stdio和HTTP
  • 符合MCP标准 -实施MCP协议版本 2024-11-05
  • 客户包括 -以编程方式连接到任何MCP服务器

安装

npm install toolcall zod

快速开始

只需几行代码即可创建MCP服务器:

import { serve, tool } from 'toolcall'
import { z } from 'zod'

serve({
  name: 'my-server',
  version: '1.0.0',
  tools: {
    greet: tool({
      description: 'Greet someone by name',
      parameters: z.object({
        name: z.string().describe('The name of the person to greet')
      }),
      execute: ({ name }) => `Hello, ${name}!`
    }),

    add: tool({
      description: 'Add two numbers',
      parameters: z.object({
        a: z.number().describe('First number'),
        b: z.number().describe('Second number')
      }),
      execute: ({ a, b }) => ({ result: a + b })
    })
  }
})

运行它:

npx tsx server.ts

Claude代码集成

工具调用服务器与 克劳德代码.将您的服务器添加到Claude Code的MCP配置中:

1.创建服务器文件

// my-tools.ts
import { serve, tool } from 'toolcall'
import { z } from 'zod'

serve({
  name: 'my-tools',
  tools: {
    get_weather: tool({
      description: 'Get current weather for a city',
      parameters: z.object({
        city: z.string().describe('City name'),
        unit: z.enum(['celsius', 'fahrenheit']).default('celsius')
      }),
      execute: async ({ city, unit }) => {
        // Your implementation here
        return { city, temperature: 22, unit, condition: 'sunny' }
      }
    })
  }
})

2.配置克劳德代码

添加到您的Claude Code MCP设置中(~/.claude/claude_desktop_config.json 或通过克劳德代码设置):

{
  "mcpServers": {
    "my-tools": {
      "command": "npx",
      "args": ["tsx", "/path/to/my-tools.ts"]
    }
  }
}

或者,如果你已经编译了TypeScript:

{
  "mcpServers": {
    "my-tools": {
      "command": "node",
      "args": ["/path/to/my-tools.js"]
    }
  }
}

3.在克劳德代码中使用

配置后,Claude Code将自动发现您的工具。你可以让克劳德使用它们:

“使用我的天气工具查看东京的天气”

API 参考

serve(options)

创建并启动MCP服务器。

serve({
  name: 'my-server',        // Server name (default: 'toolcall-server')
  version: '1.0.0',         // Server version (default: '1.0.0')
  transport: 'stdio',       // Transport type: 'stdio' | 'http' (default: 'stdio')
  port: 3000,               // Port for HTTP transport (default: 3000)
  tools: {                  // Tool definitions
    // ... your tools
  }
})

tool(definition)

定义一个具有Zod模式验证的类型安全工具。

tool({
  description: 'Tool description shown to clients',
  parameters: z.object({
    // Zod schema for parameters
  }),
  execute: async (params) => {
    // Tool implementation
    // Can return string, object, or any JSON-serializable value
  }
})

参数类型

toolcall支持所有Zod类型:

import { z } from 'zod'

// Strings
z.string()
z.string().min(1).max(100)
z.string().email()
z.string().url()

// Numbers
z.number()
z.number().min(0).max(100)
z.number().int()

// Booleans
z.boolean()

// Enums
z.enum(['option1', 'option2', 'option3'])

// Arrays
z.array(z.string())

// Optional with defaults
z.string().optional()
z.number().default(10)

// Descriptions (shown in tool schema)
z.string().describe('Parameter description')

返回值

工具可以返回任何JSON可序列化值:

// String return
execute: ({ name }) => `Hello, ${name}!`

// Object return (automatically JSON-stringified)
execute: ({ a, b }) => ({ result: a + b, operation: 'addition' })

// Async operations
execute: async ({ url }) => {
  const response = await fetch(url)
  return await response.json()
}

运输

标准(默认)

stdio传输从stdin读取JSON-RPC消息,并将响应写入stdout。这是Claude Code和其他MCP客户端使用的MCP服务器的标准传输。

serve({
  transport: 'stdio',  // or omit - stdio is default
  tools: { /* ... */ }
})

超文本传输协议

HTTP传输创建了一个接受JSON-RPC POST请求的HTTP服务器。

serve({
  transport: 'http',
  port: 3000,
  tools: { /* ... */ }
})

卷曲测试:

# Initialize
curl -X POST http://localhost:3000 \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{}}}'

# List tools
curl -X POST http://localhost:3000 \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'

# Call a tool
curl -X POST http://localhost:3000 \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"greet","arguments":{"name":"World"}}}'

客户端使用情况

toolcall包括一个用于连接到任何MCP服务器的客户端:

import { connect } from 'toolcall'

// Connect to a stdio server
const client = await connect('npx tsx ./server.ts')

// Or connect to an HTTP server
const client = await connect('http://localhost:3000')

// List available tools
console.log(client.listTools())

// Call a tool
const result = await client.call('greet', { name: 'World' })
console.log(result)  // "Hello, World!"

// Clean up
client.close()

完整示例

import { serve, tool } from 'toolcall'
import { z } from 'zod'

serve({
  name: 'example-server',
  version: '1.0.0',
  tools: {
    // Simple string return
    greet: tool({
      description: 'Greet someone by name',
      parameters: z.object({
        name: z.string().describe('The name of the person to greet')
      }),
      execute: ({ name }) => `Hello, ${name}!`
    }),

    // Object return
    add: tool({
      description: 'Add two numbers together',
      parameters: z.object({
        a: z.number().describe('First number'),
        b: z.number().describe('Second number')
      }),
      execute: ({ a, b }) => ({ result: a + b })
    }),

    // Async with enum and default
    get_weather: tool({
      description: 'Get the current weather for a city',
      parameters: z.object({
        city: z.string().describe('City name'),
        unit: z.enum(['celsius', 'fahrenheit']).default('celsius').describe('Temperature unit')
      }),
      execute: async ({ city, unit }) => {
        // Simulate API call
        const temp = Math.round(Math.random() * 30 + 10)
        const tempInUnit = unit === 'fahrenheit' ? Math.round(temp * 9 / 5 + 32) : temp
        return {
          city,
          temperature: tempInUnit,
          unit,
          condition: ['sunny', 'cloudy', 'rainy'][Math.floor(Math.random() * 3)]
        }
      }
    }),

    // Constrained parameters
    search: tool({
      description: 'Search for information',
      parameters: z.object({
        query: z.string().describe('Search query'),
        limit: z.number().min(1).max(100).default(10).describe('Maximum results')
      }),
      execute: async ({ query, limit }) => {
        return {
          query,
          results: Array.from({ length: Math.min(limit, 3) }, (_, i) => ({
            title: `Result ${i + 1} for "${query}"`,
            url: `https://example.com/result/${i + 1}`
          }))
        }
      }
    })
  }
})

错误处理

toolcall会根据您的Zod模式自动验证参数。无效参数返回JSON-RPC错误:

{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -32602,
    "message": "Invalid parameters",
    "data": {
      "name": { "_errors": ["Required"] }
    }
  }
}

工具执行中抛出的错误会被捕获并作为内部错误返回:

{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -32603,
    "message": "Internal error",
    "data": "Error message here"
  }
}

协议细节

工具调用实现了 模型上下文协议 规范:

  • 协议版本: 2024-11-05
  • 运输:基于stdio或HTTP的JSON-RPC 2.0
  • 方法:

- initialize -服务器初始化握手 - notifications/initialized -客户端初始化确认 - tools/list -列出可用工具 - tools/call -执行工具 - ping -健康检查

发展

# Install dependencies
npm install

# Build
npm run build

# Watch mode
npm run dev

# Run example server
npx tsx examples/server.ts

# Run tests
npm test

许可证

麻省理工学院

作者

杨义民(https://www.yiminyang.dev)

目录标签

目录标签

服务器开发API集成TypeScriptClaudeJavaScriptMCP协议本地部署JSON-RPCAPI工具

支持客户端

Claude

接入字段

传输方式(transport,传输协议)

stdio

鉴权方式(authType,认证方式)

none

运行时(runtime,运行环境)

Node.js

来源包(packageName,安装包名)

tsx

工具数量(toolCount,工具数)

4

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

stdionone部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

来源信息

继续浏览同类 MCP