@phake/mcp 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,16 +2,18 @@
2
2
 
3
3
  A TypeScript library for building MCP (Model Context Protocol) servers, designed to run on Cloudflare Workers and Node.js.
4
4
 
5
+ [![npm](https://img.shields.io/npm/v/@phake/mcp?style=flat-square)](https://www.npmjs.com/package/@phake/mcp)
6
+
5
7
  ## Features
6
8
 
7
- - **Multi-runtime support** Deploy to Cloudflare Workers (itty-router) or Node.js (Hono)
8
- - **OAuth 2.0 authentication** Full PKCE flow with dynamic token resolution and proactive refresh
9
- - **5 auth strategies** `oauth`, `bearer`, `api_key`, `custom`, `none`
10
- - **Tool registration** Define and register MCP tools with Zod input/output schemas
11
- - **Session & token management** Persistent storage with LRU eviction and TTL
12
- - **Multiple storage backends** Cloudflare KV, SQLite, file-based, and in-memory
13
- - **CORS support** Configurable CORS headers
14
- - **Type-safe** Full TypeScript support with Zod validation
9
+ - **Multi-runtime support** - Deploy to Cloudflare Workers (itty-router) or Node.js (Hono)
10
+ - **OAuth 2.0 authentication** - Full PKCE flow with dynamic token resolution and proactive refresh
11
+ - **5 auth strategies** - `oauth`, `bearer`, `api_key`, `custom`, `none`
12
+ - **Tool registration** - Define and register MCP tools with Zod input/output schemas
13
+ - **Session & token management** - Persistent storage with LRU eviction and TTL
14
+ - **Multiple storage backends** - Cloudflare KV, SQLite, file-based, and in-memory
15
+ - **CORS support** - Configurable CORS headers
16
+ - **Type-safe** - Full TypeScript support with Zod validation
15
17
 
16
18
  ## Installation
17
19
 
@@ -33,9 +35,7 @@ const server = createMCPServer({
33
35
  ],
34
36
  });
35
37
 
36
- export default {
37
- fetch: (request: Request, env: unknown) => server.fetch(request, env),
38
- };
38
+ export default server;
39
39
  ```
40
40
 
41
41
  ### Node.js
@@ -75,11 +75,11 @@ Creates an MCP server instance.
75
75
 
76
76
  | Strategy | Description | Config Env Vars |
77
77
  |----------|-------------|-----------------|
78
- | `oauth` | Full OAuth 2.1 PKCE flow with RS token provider token mapping | `OAUTH_CLIENT_ID`, `OAUTH_CLIENT_SECRET`, `OAUTH_SCOPES`, `OAUTH_REDIRECT_URI`, `PROVIDER_CLIENT_ID`, `PROVIDER_CLIENT_SECRET`, `PROVIDER_ACCOUNTS_URL` |
78
+ | `oauth` | Full OAuth 2.1 PKCE flow with RS token -> provider token mapping | `OAUTH_CLIENT_ID`, `OAUTH_CLIENT_SECRET`, `OAUTH_SCOPES`, `OAUTH_REDIRECT_URI`, `PROVIDER_CLIENT_ID`, `PROVIDER_CLIENT_SECRET`, `PROVIDER_ACCOUNTS_URL` |
79
79
  | `bearer` | Static Bearer token | `BEARER_TOKEN` |
80
80
  | `api_key` | Static API key in custom header (default: `x-api-key`) | `API_KEY`, `API_KEY_HEADER` |
81
81
  | `custom` | Arbitrary custom headers | `CUSTOM_HEADERS` |
82
- | `none` | No authentication required | |
82
+ | `none` | No authentication required | - |
83
83
 
84
84
  ## Storage Backends
85
85
 
@@ -92,6 +92,98 @@ Creates an MCP server instance.
92
92
  | `FileTokenStore` | RS token mappings | JSON file persistence, AES-256-GCM encryption, secure permissions (0600) |
93
93
  | `SqliteSessionStore` | Sessions | SQLite via better-sqlite3 + Drizzle ORM, WAL mode, atomic transactions |
94
94
 
95
+ ## Creating Tools
96
+
97
+ Use `defineTool` to create a type-safe tool definition, then pass it to `tools` when creating the server.
98
+
99
+ ```typescript
100
+ import { z } from "zod";
101
+ import { defineTool } from "@phake/mcp";
102
+
103
+ const greetTool = defineTool({
104
+ name: "greet",
105
+ title: "Greet User",
106
+ description: "Returns a greeting for the given name",
107
+ inputSchema: z.object({
108
+ name: z.string().describe("Name to greet"),
109
+ }),
110
+ outputSchema: {
111
+ message: z.string().describe("The greeting message"),
112
+ },
113
+ annotations: {
114
+ readOnlyHint: true,
115
+ destructiveHint: false,
116
+ idempotentHint: true,
117
+ },
118
+ handler: async (args, context) => {
119
+ const message = `Hello, ${args.name}!`;
120
+ return {
121
+ content: [{ type: "text", text: message }],
122
+ structuredContent: { message },
123
+ };
124
+ },
125
+ });
126
+ ```
127
+
128
+ ### Authenticated Tools
129
+
130
+ Set `requiresAuth: true` to have the dispatcher automatically reject calls when no token is present. Use `context.resolvedHeaders` to call external APIs.
131
+
132
+ ```typescript
133
+ import { defineTool } from "@phake/mcp";
134
+ import { z } from "zod";
135
+
136
+ const profileTool = defineTool({
137
+ name: "get_profile",
138
+ description: "Fetch the authenticated user's profile",
139
+ inputSchema: z.object({}),
140
+ requiresAuth: true,
141
+ handler: async (_args, context) => {
142
+ const response = await fetch("https://api.example.com/me", {
143
+ headers: context.resolvedHeaders,
144
+ });
145
+ const data = await response.json();
146
+ return {
147
+ content: [{ type: "text", text: JSON.stringify(data) }],
148
+ };
149
+ },
150
+ });
151
+ ```
152
+
153
+ ### Registering Tools
154
+
155
+ ```typescript
156
+ import { createMCPServer } from "@phake/mcp";
157
+
158
+ const server = createMCPServer({
159
+ adapter: "worker",
160
+ tools: [greetTool, profileTool],
161
+ });
162
+ ```
163
+
164
+ ### Tool Handler Reference
165
+
166
+ | Field | Type | Required | Description |
167
+ |-------|------|----------|-------------|
168
+ | `name` | `string` | Yes | Unique tool name |
169
+ | `description` | `string` | Yes | Description shown to the LLM |
170
+ | `inputSchema` | `ZodObject` | Yes | Zod schema for input validation |
171
+ | `outputSchema` | `ZodRawShape` | No | Zod schema for structured output |
172
+ | `handler` | `function` | Yes | `(args, context) => Promise<ToolResult>` |
173
+ | `requiresAuth` | `boolean` | No | Reject calls without a provider token |
174
+ | `title` | `string` | No | Human-readable display title |
175
+ | `annotations` | `object` | No | MCP hints (`readOnlyHint`, `destructiveHint`, etc.) |
176
+
177
+ The `context` object provides:
178
+
179
+ | Property | Description |
180
+ |----------|-------------|
181
+ | `sessionId` | Current MCP session ID |
182
+ | `providerToken` | Access token for external API calls |
183
+ | `resolvedHeaders` | Ready-to-use auth headers for `fetch` |
184
+ | `authStrategy` | Active auth strategy |
185
+ | `signal` | `AbortSignal` for cancellation |
186
+
95
187
  ## Built-in Tools
96
188
 
97
189
  | Tool | Input | Output | Description |
@@ -170,13 +262,13 @@ src/
170
262
 
171
263
  ## Dependencies
172
264
 
173
- - `@modelcontextprotocol/sdk` MCP protocol implementation
174
- - `zod` Schema validation
175
- - `jose` JWT and JWS/JWE
176
- - `oauth4webapi` OAuth 2.0 for Web API
177
- - `itty-router` Lightweight router (Workers)
178
- - `hono` / `@hono/node-server` Node.js HTTP framework (optional)
179
- - `drizzle-orm` / `better-sqlite3` Database layer (optional)
265
+ - `@modelcontextprotocol/sdk` - MCP protocol implementation
266
+ - `zod` - Schema validation
267
+ - `jose` - JWT and JWS/JWE
268
+ - `oauth4webapi` - OAuth 2.0 for Web API
269
+ - `itty-router` - Lightweight router (Workers)
270
+ - `hono` / `@hono/node-server` - Node.js HTTP framework (optional)
271
+ - `drizzle-orm` / `better-sqlite3` - Database layer (optional)
180
272
 
181
273
  ## Inspired By
182
274
 
@@ -184,4 +276,4 @@ This project was inspired by [streamable-mcp-server-template](https://github.com
184
276
 
185
277
  ## License
186
278
 
187
- This project is licensed under the MIT License see the [LICENSE](LICENSE) file for details.
279
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.