npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mcp-server-js

v1.0.0

Published

Create MCP servers in JavaScript and TypeScript with a simple API.

Readme

MCP Server JS

npm version License: MIT

A simple and elegant TypeScript/JavaScript library for building Model Context Protocol (MCP) servers with a minimal API surface.

Features

Simple API - Create MCP servers with just a few lines of code
🔧 Type-Safe - Full TypeScript support with Zod validation
📦 Zero Dependencies - Minimal production dependencies
🚀 Multiple Transports - HTTP, stdio, and WebSocket support
Fully Tested - Comprehensive test suite

Installation

npm install mcp-server-js

Quick Start

Basic Example

import { createMCP } from "mcp-server-js"

const app = createMCP({ debug: true })

// Register a simple tool
app.tool({
  name: "greet",
  handler: async ({ name }) => {
    return `Hello, ${name}!`
  }
})

app.start()

// Execute a tool
const result = await app.execute("greet", { 
  name: "World" 
})
console.log(result) // "Hello, World!"

With Input Validation

import { createMCP } from "mcp-server-js"
import { z } from "zod"

const app = createMCP()

app.tool({
  name: "add",
  schema: z.object({
    a: z.number(),
    b: z.number()
  }),
  handler: async ({ a, b }) => {
    return { result: a + b }
  }
})

app.start()

// Inputs are validated automatically
const result = await app.execute("add", { 
  a: 5, 
  b: 3 
})

API Reference

createMCP(options?)

Creates a new MCP server instance.

const app = createMCP({
  debug: true // Enable debug logging
})

app.tool(toolDefinition)

Registers a tool on the server.

app.tool({
  name: "tool-name",
  schema: z.object({...}), // Optional: input validation schema
  handler: async (input) => {
    // Tool implementation
    return result
  }
})

app.execute(toolName, input?)

Executes a registered tool.

const result = await app.execute("tool-name", { 
  /* input */ 
})

app.listTools()

Returns an array of all registered tools.

const tools = app.listTools()
tools.forEach(tool => console.log(tool.name))

app.start()

Starts the MCP server.

app.start()

Transports

HTTP Server

import { createMCP, createHttpServer } from "mcp-server-js"

const app = createMCP()

app.tool({
  name: "hello",
  handler: async () => "Hello!"
})

const httpServer = createHttpServer(app, { 
  port: 3000 
})

await httpServer.listen()

Available endpoints:

  • GET /tools - List all tools
  • POST /execute - Execute a tool

Stdio

import { 
  createMCP, 
  attachStdio 
} from "mcp-server-js"

const app = createMCP()

app.tool({
  name: "hello",
  handler: async () => "Hello!"
})

attachStdio(app)

Examples

See the /examples directory for complete examples:

  • basic.ts - Simple server with greeting and calculation tools
  • weather.ts - Tool with simulated weather data
  • multi-tools.ts - Server with multiple math and string tools

Run examples:

npm run example:basic
npm run example:weather
npm run example:multi

Testing

Run the comprehensive test suite:

npm test

The test suite covers:

  • ✅ Server creation
  • ✅ Tool registration
  • ✅ Input validation
  • ✅ Async/sync handlers
  • ✅ Error handling
  • ✅ Tool listing

Development

Build

npm run build

Watch Mode

npm run dev

Package.json Scripts

{
  "scripts": {
    "build": "tsup src/index.ts --format esm,cjs --dts",
    "dev": "tsup src/index.ts --watch",
    "test": "node --loader tsx/cjs tests/server.test.ts",
    "example:basic": "node --loader tsx/cjs examples/basic.ts"
  }
}

Project Structure

mcp-server-js/
├── src/
│   ├── core/              # Core MCP server logic
│   │   ├── MCPServer.ts   # Main server class
│   │   ├── createMCP.ts   # Factory function
│   │   └── registry.ts    # Tool registry
│   ├── tool/              # Tool management
│   │   ├── createTool.ts
│   │   ├── executeTool.ts
│   │   └── validateInput.ts
│   ├── transport/         # Transport implementations
│   │   ├── http.ts
│   │   ├── stdio.ts
│   │   └── websocket.ts
│   ├── utils/             # Utilities
│   │   ├── errors.ts
│   │   └── logger.ts
│   ├── types/             # Type definitions
│   │   └── index.ts
│   └── index.ts           # Main export
├── examples/              # Usage examples
├── tests/                 # Test suite
└── package.json

Type Definitions

Full TypeScript support with exported types:

export type Tool = ToolDefinition
export type ToolHandler<T = any> = (input: T) => Promise<any> | any
export type ToolDefinition<T = any> = {
  name: string
  schema?: z.ZodSchema<T>
  handler: ToolHandler<T>
}
export type MCPOptions = {
  debug?: boolean
}

Error Handling

The library provides custom error types:

import { MCPError } from "mcp-server-js"

try {
  await app.execute("nonexistent")
} catch (error) {
  if (error instanceof MCPError) {
    console.error("MCP Error:", error.message)
  }
}

License

MIT © 2026

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues, questions, or suggestions, please open an issue on GitHub.


Built with ❤️ for the AI community