Update prompts.mdx
This commit is contained in:
parent
f137a03a2e
commit
de7ee4337a
1 changed files with 156 additions and 206 deletions
|
|
@ -3,7 +3,7 @@ title: "Prompts"
|
||||||
description: "Create resuable prompt templates and workflows"
|
description: "Create resuable prompt templates and workflows"
|
||||||
---
|
---
|
||||||
|
|
||||||
MCP Prompts enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs. They provide a powerful way to standardize and share common LLM interactions.
|
Prompts enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs. They provide a powerful way to standardize and share common LLM interactions.
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
|
|
@ -78,15 +78,13 @@ To use a prompt, clients make a `prompts/get` request:
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
{
|
{
|
||||||
description?: string, // Optional context about the prompt
|
description: "Analyze Python code for potential improvements",
|
||||||
messages: [ // Messages to send to the LLM
|
messages: [
|
||||||
{
|
{
|
||||||
role: "user" | "assistant",
|
role: "user",
|
||||||
content: {
|
content: {
|
||||||
type: "text" | "image",
|
type: "text",
|
||||||
text?: string,
|
text: "Please analyze the following Python code for potential improvements:\n\n```python\ndef calculate_sum(numbers):\n total = 0\n for num in numbers:\n total = total + num\n return total\n\nresult = calculate_sum([1, 2, 3, 4, 5])\nprint(result)\n```"
|
||||||
data?: string, // base64 for images
|
|
||||||
mimeType?: string
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
@ -97,57 +95,63 @@ To use a prompt, clients make a `prompts/get` request:
|
||||||
|
|
||||||
Prompts can be dynamic and include:
|
Prompts can be dynamic and include:
|
||||||
|
|
||||||
### Context integration
|
### Embedded resource context
|
||||||
|
|
||||||
```typescript
|
```json
|
||||||
const analyzeLogsPrompt = {
|
|
||||||
name: "analyze-logs",
|
|
||||||
description: "Analyze recent system logs",
|
|
||||||
arguments: [
|
|
||||||
{
|
{
|
||||||
name: "timeframe",
|
"name": "analyze-project",
|
||||||
description: "Time period to analyze",
|
"description": "Analyze project logs and code",
|
||||||
required: true
|
"arguments": [
|
||||||
|
{
|
||||||
|
"name": "timeframe",
|
||||||
|
"description": "Time period to analyze logs",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "fileUri",
|
||||||
|
"description": "URI of code file to review",
|
||||||
|
"required": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
};
|
|
||||||
|
|
||||||
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
|
||||||
if (request.params.name === "analyze-logs") {
|
|
||||||
const logs = await getSystemLogs(request.params.arguments?.timeframe);
|
|
||||||
return {
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: {
|
|
||||||
type: "text",
|
|
||||||
text: `Analyze these system logs and identify any issues:\n\n${logs}`
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Resource references
|
When handling the `prompts/get` request:
|
||||||
|
|
||||||
```typescript
|
```json
|
||||||
const codeReviewPrompt = {
|
|
||||||
name: "review-code",
|
|
||||||
async getMessages(fileUri: string) {
|
|
||||||
const code = await readResource(fileUri);
|
|
||||||
return [
|
|
||||||
{
|
{
|
||||||
role: "user",
|
"messages": [
|
||||||
content: {
|
{
|
||||||
type: "text",
|
"role": "user",
|
||||||
text: `Review this code for best practices:\n\n${code}`
|
"content": {
|
||||||
|
"type": "text",
|
||||||
|
"text": "Analyze these system logs and the code file for any issues:"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": {
|
||||||
|
"type": "resource",
|
||||||
|
"resource": {
|
||||||
|
"uri": "logs://recent?timeframe=1h",
|
||||||
|
"text": "[2024-03-14 15:32:11] ERROR: Connection timeout in network.py:127\n[2024-03-14 15:32:15] WARN: Retrying connection (attempt 2/3)\n[2024-03-14 15:32:20] ERROR: Max retries exceeded",
|
||||||
|
"mimeType": "text/plain"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
];
|
},
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": {
|
||||||
|
"type": "resource",
|
||||||
|
"resource": {
|
||||||
|
"uri": "file:///path/to/code.py",
|
||||||
|
"text": "def connect_to_service(timeout=30):\n retries = 3\n for attempt in range(retries):\n try:\n return establish_connection(timeout)\n except TimeoutError:\n if attempt == retries - 1:\n raise\n time.sleep(5)\n\ndef establish_connection(timeout):\n # Connection implementation\n pass",
|
||||||
|
"mimeType": "text/x-python"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Multi-step workflows
|
### Multi-step workflows
|
||||||
|
|
@ -187,6 +191,8 @@ const debugWorkflow = {
|
||||||
|
|
||||||
Here's a complete example of implementing prompts in an MCP server:
|
Here's a complete example of implementing prompts in an MCP server:
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
<Tab title="TypeScript">
|
||||||
```typescript
|
```typescript
|
||||||
import { Server } from "@modelcontextprotocol/sdk/server";
|
import { Server } from "@modelcontextprotocol/sdk/server";
|
||||||
import {
|
import {
|
||||||
|
|
@ -227,6 +233,10 @@ const PROMPTS = {
|
||||||
const server = new Server({
|
const server = new Server({
|
||||||
name: "example-prompts-server",
|
name: "example-prompts-server",
|
||||||
version: "1.0.0"
|
version: "1.0.0"
|
||||||
|
}, {
|
||||||
|
capabilities: {
|
||||||
|
prompts: {}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// List available prompts
|
// List available prompts
|
||||||
|
|
@ -275,6 +285,13 @@ server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
||||||
throw new Error("Prompt implementation not found");
|
throw new Error("Prompt implementation not found");
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
</Tab>
|
||||||
|
<Tab title="Python">
|
||||||
|
```python
|
||||||
|
# Python implementation coming soon
|
||||||
|
```
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
## Best practices
|
## Best practices
|
||||||
|
|
||||||
|
|
@ -324,70 +341,3 @@ When implementing prompts:
|
||||||
- Implement timeouts
|
- Implement timeouts
|
||||||
- Consider prompt injection risks
|
- Consider prompt injection risks
|
||||||
- Document security requirements
|
- Document security requirements
|
||||||
|
|
||||||
## Advanced features
|
|
||||||
|
|
||||||
### Prompt chaining
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const analyzePRPrompt = {
|
|
||||||
async getMessages(prId: string) {
|
|
||||||
// Chain multiple prompts
|
|
||||||
const diffPrompt = await getPrompt("analyze-diff");
|
|
||||||
const testsPrompt = await getPrompt("review-tests");
|
|
||||||
const securityPrompt = await getPrompt("security-review");
|
|
||||||
|
|
||||||
return [
|
|
||||||
...diffPrompt.messages,
|
|
||||||
...testsPrompt.messages,
|
|
||||||
...securityPrompt.messages
|
|
||||||
];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
### Context enrichment
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const troubleshootPrompt = {
|
|
||||||
async getMessages(error: string) {
|
|
||||||
// Enrich with system context
|
|
||||||
const systemInfo = await getSystemInfo();
|
|
||||||
const recentLogs = await getRecentLogs();
|
|
||||||
const stackTrace = await parseError(error);
|
|
||||||
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: {
|
|
||||||
type: "text",
|
|
||||||
text: `Help troubleshoot this error:
|
|
||||||
Error: ${error}
|
|
||||||
System: ${systemInfo}
|
|
||||||
Logs: ${recentLogs}
|
|
||||||
Stack: ${stackTrace}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
### Interactive workflows
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const debugWorkflow = {
|
|
||||||
async getMessages(stage: string, context: any) {
|
|
||||||
switch (stage) {
|
|
||||||
case "initial":
|
|
||||||
return promptForError();
|
|
||||||
case "analysis":
|
|
||||||
return analyzeError(context.error);
|
|
||||||
case "solution":
|
|
||||||
return suggestFixes(context.analysis);
|
|
||||||
case "verification":
|
|
||||||
return verifyFix(context.solution);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
```
|
|
||||||
Loading…
Reference in a new issue