AI工具开发框架

xmcp是一个基于结构化目录的AI工具开发框架,支持工具、提示和资源的快速定义与集成,适用于构建AI驱动的应用程序。

  • by abcprintf
  • TypeScript
  • 本地部署
  • 5 星标
  • 更新于
在 GitHub 查看 abcprintf/mcp-server-app

使用说明

xmcp应用程序

此项目是通过以下方式创建的 创建xmcp应用程序.

入门指南

首先,运行开发服务器:

npm run dev
# or
yarn dev
# or
pnpm dev

这将使用所选的传输方法启动MCP服务器。

项目结构

此项目使用结构化方法,其中工具、提示和资源从各自的目录中自动发现:

  • src/tools -工具定义
  • src/prompts -提示模板
  • src/resources -资源处理程序

工具

每个工具都在自己的文件中定义,其结构如下:

import { z } from "zod";
import { type InferSchema, type ToolMetadata } from "xmcp";

export const schema = {
  name: z.string().describe("The name of the user to greet"),
};

export const metadata: ToolMetadata = {
  name: "greet",
  description: "Greet the user",
  annotations: {
    title: "Greet the user",
    readOnlyHint: true,
    destructiveHint: false,
    idempotentHint: true,
  },
};

export default function greet({ name }: InferSchema<typeof schema>) {
  return `Hello, ${name}!`;
}

提示

提示是AI交互的模板定义:

import { z } from "zod";
import { type InferSchema, type PromptMetadata } from "xmcp";

export const schema = {
  code: z.string().describe("The code to review"),
};

export const metadata: PromptMetadata = {
  name: "review-code",
  title: "Review Code",
  description: "Review code for best practices and potential issues",
  role: "user",
};

export default function reviewCode({ code }: InferSchema<typeof schema>) {
  return `Please review this code: ${code}`;
}

资源

资源通过基于URI的访问提供数据或内容:

import { z } from "zod";
import { type ResourceMetadata, type InferSchema } from "xmcp";

export const schema = {
  userId: z.string().describe("The ID of the user"),
};

export const metadata: ResourceMetadata = {
  name: "user-profile",
  title: "User Profile",
  description: "User profile information",
};

export default function handler({ userId }: InferSchema<typeof schema>) {
  return `Profile data for user ${userId}`;
}

添加新组件

添加新工具

要添加新工具,请执行以下操作:

  1. 创建新 .ts 文件在 src/tools 目录
  2. 出口A schema 使用Zod定义刀具参数的对象
  3. 出口A metadata 带有工具信息的对象
  4. 导出实现工具逻辑的默认函数

添加新提示

要添加新提示,请执行以下操作:

  1. 创建新 .ts 文件在 src/prompts 目录
  2. 出口A schema 使用Zod定义提示参数的对象
  3. 出口A metadata 具有提示信息和角色的对象
  4. 导出返回提示文本的默认函数

添加新资源

要添加新资源,请执行以下操作:

  1. 创建新 .ts 文件在 src/resources 目录
  2. 使用文件夹结构来定义URI(例如。, (users)/[userId]/profile.ts → users://{userId}/profile)
  3. 出口A schema 动态参数对象(静态资源可选)
  4. 出口A metadata 具有资源信息的对象
  5. 导出返回资源内容的默认函数

生产大楼

要构建生产项目,请执行以下操作:

npm run build
# or
yarn build
# or
pnpm build

这将编译你的TypeScript代码并将其输出到 dist 目录。

运行服务器

您可以为使用以下构建的传输运行服务器:

  • HTTP: node dist/http.js
  • 工作室: node dist/stdio.js

给定所选的传输方法,您将有一个自定义的启动脚本添加到 package.json 文件。

对于HTTP:

npm run start-http
# or
yarn start-http
# or
pnpm start-http

对于STDIO:

npm run start-stdio
# or
yarn start-stdio
# or
pnpm start-stdio

了解更多