Skip to content

Commit 7a64f97

Browse files
authored
Add: Sampling Example to README
1 parent 4621105 commit 7a64f97

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Tools](#tools)
1313
- [Prompts](#prompts)
1414
- [Completions](#completions)
15+
- [Sampling](#sampling)
1516
- [Running Your Server](#running-your-server)
1617
- [stdio](#stdio)
1718
- [Streamable HTTP](#streamable-http)
@@ -382,6 +383,37 @@ import { getDisplayName } from "@modelcontextprotocol/sdk/shared/metadataUtils.j
382383
const displayName = getDisplayName(tool);
383384
```
384385

386+
### Sampling
387+
388+
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
389+
390+
```typescript
391+
// Result sent back from LLM follow the CreateMessageSchema
392+
import {CreateMessageResult} from "@modelcontextprotocol/sdk/types.js";
393+
394+
// Async Function to send a sampling request to the LLM at top-level
395+
async function samplingExample(server: McpServer): Promise<CreateMessageResult> {
396+
const samplingText = "Text prompt to send to LLM";
397+
const result = await McpServer.server.createMessage(
398+
{
399+
messages : [{
400+
role: "user",
401+
content: {
402+
text: samplingText,
403+
type: "text"
404+
}
405+
}],
406+
maxTokens: 1000
407+
}
408+
);
409+
return result;
410+
}
411+
412+
// Sampling request just after connecting to MCP Client
413+
server.connect(transport);
414+
samplingExample(server);
415+
```
416+
385417
## Running Your Server
386418

387419
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:

0 commit comments

Comments
 (0)