Skip to content

feat(hono): add generic Env type support to createMcpHonoApp#1878

Open
dagangtj wants to merge 1 commit intomodelcontextprotocol:mainfrom
dagangtj:feat/hono-generic-env
Open

feat(hono): add generic Env type support to createMcpHonoApp#1878
dagangtj wants to merge 1 commit intomodelcontextprotocol:mainfrom
dagangtj:feat/hono-generic-env

Conversation

@dagangtj
Copy link
Copy Markdown

Summary

This PR adds generic type support to `createMcpHonoApp` to allow users to specify custom Env types, enabling proper type inference for environment bindings (like Cloudflare D1) and variables within MCP handlers.

Changes

  • Add generic type parameter `E extends BlankEnv = BlankEnv` to `createMcpHonoApp`
  • Pass generic type to `Hono` constructor for proper type inference
  • Update `Context` type to `Context` for typed middleware support
  • Add usage example in JSDoc showing Cloudflare Workers pattern
  • Add tests for generic type support and backward compatibility

Usage Example

```typescript
// With custom Env type for Cloudflare Workers
type Env = {
Bindings: {
DB: D1Database;
};
Variables: {
user: User;
};
};

const app = createMcpHonoApp();

app.get('/data', async (c) => {
const db = c.env.DB; // typed as D1Database
const user = c.get('user'); // typed as User
// ...
});
```

Backward Compatibility

This change is fully backward compatible - existing code using `createMcpHonoApp()` without a generic parameter will continue to work exactly as before (defaults to `BlankEnv`).

Related Issue

Fixes #1877

- Add generic type parameter E extends BlankEnv = BlankEnv to createMcpHonoApp
- Pass generic type to Hono<E> constructor for proper type inference
- Update Context type to Context<E> for typed middleware support
- Add usage example in JSDoc showing Cloudflare Workers pattern
- Add tests for generic type support and backward compatibility

Fixes modelcontextprotocol#1877
@dagangtj dagangtj requested a review from a team as a code owner April 11, 2026 20:19
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 11, 2026

⚠️ No Changeset found

Latest commit: 111d07c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 11, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@1878

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@1878

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@1878

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@1878

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@1878

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@1878

commit: 111d07c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createMcpHonoApp should support generics for Env (Bindings and Variables)

1 participant