본문으로 건너뛰기

MCP 서버 게시

이 예제에서는 stdio 전송을 사용하여 기본 Mastra MCPServer를 설정하고, 빌드하고, NPM에 게시하기 위해 준비하는 과정을 안내합니다.

종속성 설치
종속성 설치에 대한 직접 링크

필요한 패키지를 설치합니다:

npm install @mastra/mcp @mastra/core tsup

MCP 서버 설정
MCP 서버 설정에 대한 직접 링크

  1. 예를 들어, stdio 서버용 파일을 만듭니다.src/mastra/stdio.ts.

  2. 파일에 다음 코드를 추가합니다. 실제 Mastra Tool을 가져오고 서버 이름을 적절하게 지정하는 것을 잊지 마십시오.

    src/mastra/stdio.ts
    #!/usr/bin/env node
    import { MCPServer } from '@mastra/mcp'
    import { weatherTool } from './tools'

    const server = new MCPServer({
    name: 'my-mcp-server',
    version: '1.0.0',
    tools: { weatherTool },
    })

    server.startStdio().catch(error => {
    console.error('Error running MCP server:', error)
    process.exit(1)
    })
  3. 업데이트하세요package.json to include the bin 항목이 빌드된 서버 파일을 가리키도록 설정하고, ESM과 CJS 출력을 모두 생성하도록 서버 빌드 스크립트를 추가합니다.

    package.json
    {
    "bin": "dist/stdio.mjs",
    "scripts": {
    "build:mcp": "tsup src/mastra/stdio.ts --format esm,cjs --no-splitting --dts && echo '#!/usr/bin/env node' | cat - dist/stdio.mjs > temp && mv temp dist/stdio.mjs && chmod +x dist/stdio.mjs"
    }
    }

    빌드 명령은 ESM(.mjs) and CJS (.cjs) outputs for maximum compatibility. The shebang (#!/usr/bin/env node)가 ESM 아티팩트 앞에 추가되어 직접 실행할 수 있게 되며, bin entry points to this file.

  4. 빌드 명령을 실행합니다.

    npm run build:mcp

    그러면 서버 코드가 ESM 및 CJS 형식으로 컴파일되고 ESM 출력 파일이 실행 가능해집니다. 유닉스 계열 시스템에서는chmod +x step은 파일을 직접 실행할 수 있게 합니다. Windows 사용자는 WSL을 사용하거나 Node.js를 통해 직접 실행해야 할 수 있습니다.

NPM에 게시
NPM에 게시에 대한 직접 링크

다른 사람(또는 자신)이 MCP 서버를 사용할 수 있도록 하려면npx or as a dependency, you can publish it to NPM.

  1. NPM 계정이 있고 로그인되어 있는지 확인하세요(npm login).

  2. 패키지 이름을 확인하세요.package.json is unique and available.

  3. 빌드 후 프로젝트 루트에서 게시 명령을 실행합니다.

    npm publish --access public

    패키지 게시에 대한 자세한 내용은 다음을 참조하세요.NPM documentation.

게시된 MCP 서버 사용
게시된 MCP 서버 사용에 대한 직접 링크

게시되면 MCP 서버를 다음에서 사용할 수 있습니다.MCPClient 에서 패키지를 실행할 명령을 지정합니다. Claude desktop, Cursor, Windsurf 같은 다른 MCP 클라이언트를 사용할 수도 있습니다.

import { MCPClient } from '@mastra/mcp'

const mcp = new MCPClient({
servers: {
// Give this MCP server instance a name
yourServerName: {
command: 'npx',
args: ['-y', '@your-org-name/your-package-name@latest'], // Replace with your package name
},
},
})

// You can then get tools or toolsets from this configuration to use in your agent
const tools = await mcp.listTools()
const toolsets = await mcp.listToolsets()

참고: 조직 범위 없이 게시한 경우args might be ["-y", "your-package-name@latest"].