Skip to content

Allow routing ctx props to Server#268

Merged
threepointone merged 4 commits into
cloudflare:mainfrom
deathbyknowledge:store-ctx-props
Aug 27, 2025
Merged

Allow routing ctx props to Server#268
threepointone merged 4 commits into
cloudflare:mainfrom
deathbyknowledge:store-ctx-props

Conversation

@deathbyknowledge
Copy link
Copy Markdown
Contributor

@deathbyknowledge deathbyknowledge commented Aug 24, 2025

Since we now can have props in ExecutionContext when using an OAuthProvider, we could optionally route them to the Server and make them available as a parameter in onStart.

The opt-in is fairly straightforward, if you want to make sure your Server tracks props, you'd use:

async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
  //  ...
  const server = await getServerByName(env.MY_SERVER, 'default', { props: ctx.props });
  // Alternatively
  const response = await routePartykitRequest(request, env, { ...otherOptions, props: ctx.props })
  // ...
}

A class that extends Server can make sure the props are available during the entire lifecycle of the DO by using:

  props?: Props;
  async onStart(props?: Props) {
    // If onStart was passed props, save them to storage
    if (props) {
      await this.ctx.storage.put("props", props);
    }
    this.props = await this.ctx.storage.get("props");
    // Rest of your initialization logic...
  }

Related to cloudflare/agents#415, as it props were only implemented in McpServer

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Aug 24, 2025

⚠️ No Changeset found

Latest commit: ffb92ce

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

@deathbyknowledge deathbyknowledge changed the title Allow storing ctx props in server Allow routing ctx props to Server Aug 27, 2025
Comment thread packages/partyserver/src/index.ts Outdated
await this.ctx.blockConcurrencyWhile(async () => {
this.#status = "starting";
await this.onStart();
await this.onStart(this.#_props);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm one problem is that onStart is called before an alarm on a new DO, and props won't be available there. it's... fine. they'll have to implement it in app layer anyway (at least for now)

@threepointone threepointone merged commit 17b9bb4 into cloudflare:main Aug 27, 2025
1 check passed
threepointone added a commit that referenced this pull request Aug 27, 2025
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.

2 participants