MCP 服务器 - 用于构建 MCP 服务器的 Node In Layers 包
该库增加了使用 Node In Layers 轻松创建 MCP 服务器的功能。
它有一个名为“@node-in-layers/mcp-client”的配套库,用于创建 MCP 客户端。这两个库在定义模型和工具方面共享相同的功能。
新建图层
此库为系统添加了一个新层mcp 。它应放置在express层之后。
Related MCP server: Node Omnibus MCP Server
用法
为了使用这个库,您必须对您的配置进行添加,以及从您的应用程序/域中创建和导出“mcp”层。
配置
将此应用/域名添加到您的配置文件中。您应该在将工具添加到 MCP 服务器的应用程序之前执行此操作。
然后使用以下内容配置mcp app/domain:
const mcpConfig = {
// (optional) The name of your MCP server.
name: 'mcp',
// (optional) The version of your MCP server.
version: '1.0.0',
// The server config from @l4t/mcp-ai/simple-server/types.js
server: {
connection: {
type: 'http',
host: 'localhost',
port: 3000,
},
},
logging: {
// optional
// If you want to change the default. Its 'info' by default.
requestLogLevel: 'info',
// If you want to change the default. Its 'info' by default.
responseLogLevel: 'info',
},
}
const config = {
['@node-in-layers/mcp-server']: mcpConfig,
}创建 MCP 层
您可以通过从应用程序/域导出返回层的函数来创建 MCP 层。
// /src/yourDomain/mcp.ts
import { McpContext, McpNamespace } from '@node-in-layers/mcp-server'
import { Config } from '@node-in-layers/core'
import { YourFeaturesLayer } from './features.js'
const create = (context: McpContext<Config, YourFeaturesLayer>) => {
// Adds your tool.
context.mcp[McpNamespace].addTool({
name: 'my-hello-world-tool',
description: 'My Tool',
execute: async (input: any) => {
return 'Hello, world!'
},
})
// Create a tool from your feature
context.mcp[McpNamespace].addTool({
name: 'my-hello-world-tool',
description: 'My Tool',
inputSchema: {
type: 'object',
properties: {
name: {
type: 'string',
},
},
required: ['name'],
},
execute: (input: any) => {
// You get an object, pass it back to your feature. Handles async for you.
return context.features.yourDomain.yourFeature(input)
},
})
return {}
}
export { create }添加模型
您可以使用 CRUDS 函数包装您的模型,并通过 mcp 层将它们添加到 MCP 服务器。注意:为了实现此功能,您的层必须同时包含服务层和功能层。(除了您的模型之外。)层中的节点会自动为您的模型创建一个 cruds 属性,您可以添加它们。
下面是一个逐个执行的示例。(通常不推荐,但可行)。
// /src/yourDomain/mcp.ts
import { McpContext, McpNamespace } from '@node-in-layers/mcp-server'
import { Config } from '@node-in-layers/core'
import { YourFeaturesLayer } from './features.js'
const create = (context: McpContext<Config, YourFeaturesLayer>) => {
// Adds your models cruds through features.
context.mcp[McpNamespace].addModelCruds(
context.features.yourFeature.cruds.Cars
)
return {}
}这里有一种方法,让你真正用煤气做饭。(强烈推荐)
// /src/yourDomain/mcp.ts
import { McpContext, McpNamespace, mcpModels } from '@node-in-layers/mcp-server'
import { Config } from '@node-in-layers/core'
import { YourFeaturesLayer } from './features.js'
const create = (context: McpContext<Config, YourFeaturesLayer>) => {
// This automatically adds ALL of your models from features.
mcpModels('yourDomain')(context)
return {}
}另一种组织添加模型的方式是使用集中式 mcp 域。在所有其他域都加载完成后,将其作为最后一个域。
// /src/mcp/mcp.ts
import { McpContext, McpNamespace, mcpModels } from '@node-in-layers/mcp-server'
import { Config } from '@node-in-layers/core'
const create = (context: McpContext<Config>) => {
// Add all your models for your whole system in one go.
mcpModels('yourDomain')(context)
mcpModels('yourDomain2')(context)
mcpModels('yourDomain3')(context)
mcpModels('yourDomain4')(context)
return {}
}This server cannot be installed
Resources
Looking for Admin?
Admins can modify the Dockerfile, update the server description, and track usage metrics. If you are the server author, to authenticate as an admin.