Skip to content

Controlling Context in Client for Sampling Requests #1205

Closed

Description

@yarnabrina

Question

I am exploring the sampling components in MCP. Based on my understanding of the protocol, this should allow the server to utilise the LLM(s) available on the client side. Unfortunately, none of the examples provide a working demonstration of this.

I have managed to get something working here, but I had to invoke the LLM directly using only part of the information from the CreateMessageRequestParams object. I have not been able to determine the purpose of RequestContext or how it can be leveraged.

In summary, these are my questions:

  1. How can I access the LLM(s) available to the client directly, rather than making an explicit call?
  2. How can I use modelPreferences to suggest that the client changes the choice of LLM based on priorities or name hints?
  3. How can I use the RequestContext to pass available context to the LLM for improved generation?
  4. How can I use the includeContext attribute of CreateMessageRequestParams to filter what information is included in the context?

Any guidance or examples would be greatly appreciated.

Additional Context

  • MCP Python SDK version: 1.12.2
  • Python version: 3.13.5
  • Operating System: Ubuntu 22.04.5 LTS on Windows 11 Home via WSL 2

Activity

  1. yarnabrina commented on Aug 5, 2025

    @yarnabrina
    Author

    @dsp-ant @ihrpr @Kludex may I kindly request you to have a look at this question when you have a moment? I'd appreciate your inputs.

  2. Kludex commented on Aug 5, 2025

    @Kludex
    Member

    I am exploring the sampling components in MCP. Based on my understanding of the protocol, this should allow the server to utilise the LLM(s) available on the client side. Unfortunately, none of the examples provide a working demonstration of this.

    Check https://ai.pydantic.dev/mcp/server/#mcp-sampling.

  3. yarnabrina commented on Aug 5, 2025

    @yarnabrina
    Author

    Thanks for the response. Sorry if I was unclear, what I meant was none of the examples actually show how does the MCP client callback calls already configured LLM (or use hints to switch the model). I am not using pydantic-ai yet, but in that example also assumes a LLM response as a TODO comment only.

    As linked, I do have a sample implementation, but I am not sure if this is the intended way to use as I failed to use some properties, some guidance will be much appreciated.

    https://github.com/yarnabrina/learn-model-context-protocol/blob/770728148a8aa423a1467785bdb695927cf3428d/src/mcp_learning/mcp_client/orchestrator.py#L253-L334

  4. added
    documentationImprovements or additions to documentation
    ready for workEnough information for someone to start working on
    P2Moderate issues affecting some users, edge cases, potentially valuable feature
    P1Significant bug affecting many users, highly requested feature
    and removed
    questionFurther information is requested
    P2Moderate issues affecting some users, edge cases, potentially valuable feature
    on Oct 3, 2025
  5. felixweinberger commented on Oct 3, 2025

    @felixweinberger
    Contributor

    Should add a sampling example to our mkdocs

  6. yarnabrina commented on Oct 4, 2025

    @yarnabrina
    Author

    @felixweinberger thanks for looking into it. Can you please have a look at above link and see if this is the ideal way to use sampling? If so, I'm happy to send a PR if you can guide me about the docs/examples contribution process here.

  7. maxisbey commented on Oct 6, 2025

    @maxisbey
    Contributor

    You could start with a draft PR by adding an example server for sampling, and an example client for sampling in the examples/ folder, each with a README.md stating how to run the tests. We're in the process of adding a proper documentation structure so you can also try to add some docs in the docs/ folder as well if you'd like.

    Give it your best shot then it makes it easy for someone to give feedback afterwards if anything needs to be changed :)

  8. added a commit that references this issue on Mar 2, 2026
    02c4c1a
  9. ayzmkk-rongbi commented on Mar 2, 2026

    @ayzmkk-rongbi

    I've submitted a PR to address this: #2194

    It adds:

    • A complete sampling example with both server and client in examples/
    • A documentation page (docs/sampling.md) explaining the sampling flow, client-side callback setup, and integration with LLM providers (OpenAI, Anthropic)
    • Navigation update in mkdocs.yml

    Happy to incorporate any feedback!

  10. added 2 commits that reference this issue on Mar 2, 2026
    51cbe1b
    9b2defc
  11. trentisiete commented on Apr 19, 2026

    @trentisiete

    Hi all — #1436 is still open on this but has been idle since late 2025 after @felixweinberger's review (drop the openai SDK dep, refactor behind an LLMClient abstraction à la simple-chatbot).

    I took a shot at an alternative that addresses that feedback:

    • Provider-agnostic: speaks OpenAI /chat/completions via httpx — runs against OpenAI, Groq, OpenRouter, Ollama, vLLM. No provider SDK in deps; swap via env vars (LLM_API_KEY / LLM_API_BASE_URL / LLM_MODEL).
    • Small LLMClient wrapper so ModelPreferences hints/priorities handling is isolated, matching the simple-chatbot pattern.
    • Companion server that exercises every advisory field of sampling/createMessage (hints, cost/speed/intelligence priorities, systemPrompt, temperature, maxTokens, stopSequences, includeContext, metadata) so the client side demonstrates how each one is interpreted.
    • Verified end-to-end against a stubbed OpenAI-compatible endpoint — the full handshake runs, the first ModelHint is honoured as a soft override, every advisory field is forwarded, provider failures surface as ErrorData. ruff and pyright clean.

    Diff on my fork: main...trentisiete:python-sdk:feat/simple-sampling-example

    I don't want to step on #1436. @yarnabrina — happy to contribute any of this into your PR instead if that works for you. If maintainers (@felixweinberger, @maxisbey) would rather I open a fresh PR, glad to do that.

  12. yarnabrina commented on Apr 19, 2026

    @yarnabrina
    Author

    Hi @trentisiete , I did address the feedbacks, but even after multiple pings got no review or responses from anyone. I doubt maintainers are active and/or tracking old PR's. If you want to contribute in my fork or in a separate PR please go ahead, O don't mind, documentation helps everyone.

  13. He-wei-gui commented on Jun 6, 2026

    @He-wei-gui

    I opened #2789 with a smaller docs-only approach for this issue.

    It explains how client-side sampling_callback handlers receive CreateMessageRequestParams, including model_preferences, include_context, and ClientRequestContext. I kept it provider-neutral so it complements the existing server-side sampling snippet without adding a full example project or model-provider dependency.

  14. prajvalrasik commented on Jun 20, 2026

    @prajvalrasik

    I'd like to work on this. I have a focused docs/example update for the client sampling callback that covers model preferences, request context, includeContext, and where to call a local or hosted LLM. Could I be assigned?

    Disclosure: AI assistance was used while preparing the draft; I reviewed the change and take responsibility for it.

  15. maxisbey commented on Jul 29, 2026

    @maxisbey
    Contributor

    Thanks for the report. Closing this as it's been fixed in v2, which is now released (see #2957). If it still reproduces on v2, feel free to reopen.

    AI Disclaimer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Significant bug affecting many users, highly requested featuredocumentationImprovements or additions to documentationgood first issueGood for newcomersready for workEnough information for someone to start working on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions