Skip to content

Added Sampling Example to README #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Tools](#tools)
- [Prompts](#prompts)
- [Completions](#completions)
- [Sampling](#sampling)
- [Running Your Server](#running-your-server)
- [stdio](#stdio)
- [Streamable HTTP](#streamable-http)
Expand Down Expand Up @@ -382,6 +383,37 @@ import { getDisplayName } from "@modelcontextprotocol/sdk/shared/metadataUtils.j
const displayName = getDisplayName(tool);
```

### Sampling

MCP servers can also request MCP client LLMs for responses. Below is an example of a sampling request sent just after connecting to the Client

```typescript
// Result sent back from LLM follow the CreateMessageSchema
import {CreateMessageResult} from "@modelcontextprotocol/sdk/types.js";

// Async Function to send a sampling request to the LLM at top-level
async function samplingExample(server: McpServer): Promise<CreateMessageResult> {
const samplingText = "Example Sampling Prompt";
const result = await McpServer.server.createMessage(
{
messages : [{
role: "user",
content: {
text: samplingText,
type: "text"
}
}],
maxTokens: 1000
}
);
return result;
}

// Sampling request just after connecting to MCP Client
server.connect(transport);
samplingExample(server);
```

## Running Your Server

MCP servers in TypeScript need to be connected to a transport to communicate with clients. How you start the server depends on the choice of transport:
Expand Down