Skip to content

Make ExportedHandler support setting ExecutionContext<Props> #501

@kentcdodds

Description

@kentcdodds

Here's what I like to do:

export default {
	fetch: async (request, env, ctx) => {
		const url = new URL(request.url)

		if (url.pathname === '/mcp') {
			ctx.props.baseUrl = url.origin

			const mcp = EpicMeMCP.serve('/mcp', {
				binding: 'EPIC_ME_MCP_OBJECT',
			})
			return mcp.fetch(request, env, ctx)
		}

		return new Response('Not found', { status: 404 })
	},
} satisfies ExportedHandler<Env>

The satisfies ExportedHandler<Env> makes it so I don't have to manually type my fetch, but I'm getting an error when setting ctx.props.baseUrl because ctx.props is unknown. Here's my workaround:

export default {
	fetch: async (request: Request, env: Env, ctx: ExecutionContext<Props>) => {
		const url = new URL(request.url)

		if (url.pathname === '/mcp') {
			ctx.props.baseUrl = url.origin

			const mcp = EpicMeMCP.serve('/mcp', {
				binding: 'EPIC_ME_MCP_OBJECT',
			})
			return mcp.fetch(request, env, ctx)
		}

		return new Response('Not found', { status: 404 })
	},
}

I don't like the workaround as much.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requeston the roadmapFeature accepted and planned for implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions