Skip to content

Commit e35e8a6

Browse files
Merge branch 'main' into patch-1
2 parents ee04433 + 503202b commit e35e8a6

12 files changed

Lines changed: 633 additions & 51 deletions

File tree

.github/workflows/release.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,3 @@ jobs:
212212
--title "Release $VERSION" \
213213
--notes-file RELEASE_NOTES.md
214214
215-
- name: Docker MCP images
216-
uses: peter-evans/repository-dispatch@v3
217-
with:
218-
token: ${{ secrets.DOCKER_TOKEN }}
219-
repository: docker/labs-ai-tools-for-devs
220-
event-type: build-mcp-images
221-
client-payload: '{"ref": "${{ needs.create-metadata.outputs.version }}"}'

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

README.md

Lines changed: 189 additions & 36 deletions
Large diffs are not rendered by default.

src/everything/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ This MCP server attempts to exercise all the features of the MCP protocol. It is
7272
- Embedded resource with `type: "resource"`
7373
- Text instruction for using the resource URI
7474

75+
9. `startElicitation`
76+
- Initiates an elicitation (interaction) within the MCP client.
77+
- Inputs:
78+
- `color` (string): Favorite color
79+
- `number` (number, 1-100): Favorite number
80+
- `pets` (enum): Favorite pet
81+
- Returns: Confirmation of the elicitation demo with selection summary.
82+
7583
### Resources
7684

7785
The server provides 100 test resources in two formats:

src/everything/everything.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ const GetResourceReferenceSchema = z.object({
8686
.describe("ID of the resource to reference (1-100)"),
8787
});
8888

89+
const ElicitationSchema = z.object({});
90+
91+
const GetResourceLinksSchema = z.object({
92+
count: z
93+
.number()
94+
.min(1)
95+
.max(10)
96+
.default(3)
97+
.describe("Number of resource links to return (1-10)"),
98+
});
99+
89100
enum ToolName {
90101
ECHO = "echo",
91102
ADD = "add",
@@ -95,6 +106,8 @@ enum ToolName {
95106
GET_TINY_IMAGE = "getTinyImage",
96107
ANNOTATED_MESSAGE = "annotatedMessage",
97108
GET_RESOURCE_REFERENCE = "getResourceReference",
109+
ELICITATION = "startElicitation",
110+
GET_RESOURCE_LINKS = "getResourceLinks",
98111
}
99112

100113
enum PromptName {
@@ -116,6 +129,7 @@ export const createServer = () => {
116129
tools: {},
117130
logging: {},
118131
completions: {},
132+
elicitation: {},
119133
},
120134
instructions
121135
}
@@ -206,6 +220,21 @@ export const createServer = () => {
206220
return await server.request(request, CreateMessageResultSchema);
207221
};
208222

223+
const requestElicitation = async (
224+
message: string,
225+
requestedSchema: any
226+
) => {
227+
const request = {
228+
method: 'elicitation/create',
229+
params: {
230+
message,
231+
requestedSchema
232+
}
233+
};
234+
235+
return await server.request(request, z.any());
236+
};
237+
209238
const ALL_RESOURCES: Resource[] = Array.from({ length: 100 }, (_, i) => {
210239
const uri = `test://static/resource/${i + 1}`;
211240
if (i % 2 === 0) {
@@ -459,6 +488,17 @@ export const createServer = () => {
459488
"Returns a resource reference that can be used by MCP clients",
460489
inputSchema: zodToJsonSchema(GetResourceReferenceSchema) as ToolInput,
461490
},
491+
{
492+
name: ToolName.ELICITATION,
493+
description: "Demonstrates the Elicitation feature by asking the user to provide information about their favorite color, number, and pets.",
494+
inputSchema: zodToJsonSchema(ElicitationSchema) as ToolInput,
495+
},
496+
{
497+
name: ToolName.GET_RESOURCE_LINKS,
498+
description:
499+
"Returns multiple resource links that reference different types of resources",
500+
inputSchema: zodToJsonSchema(GetResourceLinksSchema) as ToolInput,
501+
},
462502
];
463503

464504
return { tools };
@@ -648,6 +688,91 @@ export const createServer = () => {
648688
return { content };
649689
}
650690

691+
if (name === ToolName.ELICITATION) {
692+
ElicitationSchema.parse(args);
693+
694+
const elicitationResult = await requestElicitation(
695+
'What are your favorite things?',
696+
{
697+
type: 'object',
698+
properties: {
699+
color: { type: 'string', description: 'Favorite color' },
700+
number: { type: 'integer', description: 'Favorite number', minimum: 1, maximum: 100 },
701+
pets: {
702+
type: 'string',
703+
enum: ['cats', 'dogs', 'birds', 'fish', 'reptiles'],
704+
description: 'Favorite pets'
705+
},
706+
}
707+
}
708+
);
709+
710+
// Handle different response actions
711+
const content = [];
712+
713+
if (elicitationResult.action === 'accept' && elicitationResult.content) {
714+
content.push({
715+
type: "text",
716+
text: `✅ User provided their favorite things!`,
717+
});
718+
719+
// Only access elicitationResult.content when action is accept
720+
const { color, number, pets } = elicitationResult.content;
721+
content.push({
722+
type: "text",
723+
text: `Their favorites are:\n- Color: ${color || 'not specified'}\n- Number: ${number || 'not specified'}\n- Pets: ${pets || 'not specified'}`,
724+
});
725+
} else if (elicitationResult.action === 'decline') {
726+
content.push({
727+
type: "text",
728+
text: `❌ User declined to provide their favorite things.`,
729+
});
730+
} else if (elicitationResult.action === 'cancel') {
731+
content.push({
732+
type: "text",
733+
text: `⚠️ User cancelled the elicitation dialog.`,
734+
});
735+
}
736+
737+
// Include raw result for debugging
738+
content.push({
739+
type: "text",
740+
text: `\nRaw result: ${JSON.stringify(elicitationResult, null, 2)}`,
741+
});
742+
743+
return { content };
744+
}
745+
746+
if (name === ToolName.GET_RESOURCE_LINKS) {
747+
const { count } = GetResourceLinksSchema.parse(args);
748+
const content = [];
749+
750+
// Add intro text
751+
content.push({
752+
type: "text",
753+
text: `Here are ${count} resource links to resources available in this server (see full output in tool response if your client does not support resource_link yet):`,
754+
});
755+
756+
// Return resource links to actual resources from ALL_RESOURCES
757+
const actualCount = Math.min(count, ALL_RESOURCES.length);
758+
for (let i = 0; i < actualCount; i++) {
759+
const resource = ALL_RESOURCES[i];
760+
content.push({
761+
type: "resource_link",
762+
uri: resource.uri,
763+
name: resource.name,
764+
description: `Resource ${i + 1}: ${
765+
resource.mimeType === "text/plain"
766+
? "plaintext resource"
767+
: "binary blob resource"
768+
}`,
769+
mimeType: resource.mimeType,
770+
});
771+
}
772+
773+
return { content };
774+
}
775+
651776
throw new Error(`Unknown tool: ${name}`);
652777
});
653778

src/filesystem/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,58 @@ Node.js server implementing Model Context Protocol (MCP) for filesystem operatio
99
- Move files/directories
1010
- Search files
1111
- Get file metadata
12+
- Dynamic directory access control via [Roots](https://modelcontextprotocol.io/docs/concepts/roots)
13+
14+
## Directory Access Control
15+
16+
The server uses a flexible directory access control system. Directories can be specified via command-line arguments or dynamically via [Roots](https://modelcontextprotocol.io/docs/concepts/roots).
17+
18+
### Method 1: Command-line Arguments
19+
Specify Allowed directories when starting the server:
20+
```bash
21+
mcp-server-filesystem /path/to/dir1 /path/to/dir2
22+
```
23+
24+
### Method 2: MCP Roots (Recommended)
25+
MCP clients that support [Roots](https://modelcontextprotocol.io/docs/concepts/roots) can dynamically update the Allowed directories.
26+
27+
Roots notified by Client to Server, completely replace any server-side Allowed directories when provided.
28+
29+
**Important**: If server starts without command-line arguments AND client doesn't support roots protocol (or provides empty roots), the server will throw an error during initialization.
30+
31+
This is the recommended method, as this enables runtime directory updates via `roots/list_changed` notifications without server restart, providing a more flexible and modern integration experience.
32+
33+
### How It Works
34+
35+
The server's directory access control follows this flow:
36+
37+
1. **Server Startup**
38+
- Server starts with directories from command-line arguments (if provided)
39+
- If no arguments provided, server starts with empty allowed directories
40+
41+
2. **Client Connection & Initialization**
42+
- Client connects and sends `initialize` request with capabilities
43+
- Server checks if client supports roots protocol (`capabilities.roots`)
44+
45+
3. **Roots Protocol Handling** (if client supports roots)
46+
- **On initialization**: Server requests roots from client via `roots/list`
47+
- Client responds with its configured roots
48+
- Server replaces ALL allowed directories with client's roots
49+
- **On runtime updates**: Client can send `notifications/roots/list_changed`
50+
- Server requests updated roots and replaces allowed directories again
51+
52+
4. **Fallback Behavior** (if client doesn't support roots)
53+
- Server continues using command-line directories only
54+
- No dynamic updates possible
55+
56+
5. **Access Control**
57+
- All filesystem operations are restricted to allowed directories
58+
- Use `list_allowed_directories` tool to see current directories
59+
- Server requires at least ONE allowed directory to operate
60+
61+
**Note**: The server will only allow operations within directories specified either via `args` or via Roots.
62+
1263

13-
**Note**: The server will only allow operations within directories specified via `args`.
1464

1565
## API
1666

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
2+
import { getValidRootDirectories } from '../roots-utils.js';
3+
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, realpathSync } from 'fs';
4+
import { tmpdir } from 'os';
5+
import { join } from 'path';
6+
import type { Root } from '@modelcontextprotocol/sdk/types.js';
7+
8+
describe('getValidRootDirectories', () => {
9+
let testDir1: string;
10+
let testDir2: string;
11+
let testDir3: string;
12+
let testFile: string;
13+
14+
beforeEach(() => {
15+
// Create test directories
16+
testDir1 = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-roots-test1-')));
17+
testDir2 = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-roots-test2-')));
18+
testDir3 = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-roots-test3-')));
19+
20+
// Create a test file (not a directory)
21+
testFile = join(testDir1, 'test-file.txt');
22+
writeFileSync(testFile, 'test content');
23+
});
24+
25+
afterEach(() => {
26+
// Cleanup
27+
rmSync(testDir1, { recursive: true, force: true });
28+
rmSync(testDir2, { recursive: true, force: true });
29+
rmSync(testDir3, { recursive: true, force: true });
30+
});
31+
32+
describe('valid directory processing', () => {
33+
it('should process all URI formats and edge cases', async () => {
34+
const roots = [
35+
{ uri: `file://${testDir1}`, name: 'File URI' },
36+
{ uri: testDir2, name: 'Plain path' },
37+
{ uri: testDir3 } // Plain path without name property
38+
];
39+
40+
const result = await getValidRootDirectories(roots);
41+
42+
expect(result).toContain(testDir1);
43+
expect(result).toContain(testDir2);
44+
expect(result).toContain(testDir3);
45+
expect(result).toHaveLength(3);
46+
});
47+
48+
it('should normalize complex paths', async () => {
49+
const subDir = join(testDir1, 'subdir');
50+
mkdirSync(subDir);
51+
52+
const roots = [
53+
{ uri: `file://${testDir1}/./subdir/../subdir`, name: 'Complex Path' }
54+
];
55+
56+
const result = await getValidRootDirectories(roots);
57+
58+
expect(result).toHaveLength(1);
59+
expect(result[0]).toBe(subDir);
60+
});
61+
});
62+
63+
describe('error handling', () => {
64+
65+
it('should handle various error types', async () => {
66+
const nonExistentDir = join(tmpdir(), 'non-existent-directory-12345');
67+
const invalidPath = '\0invalid\0path'; // Null bytes cause different error types
68+
const roots = [
69+
{ uri: `file://${testDir1}`, name: 'Valid Dir' },
70+
{ uri: `file://${nonExistentDir}`, name: 'Non-existent Dir' },
71+
{ uri: `file://${testFile}`, name: 'File Not Dir' },
72+
{ uri: `file://${invalidPath}`, name: 'Invalid Path' }
73+
];
74+
75+
const result = await getValidRootDirectories(roots);
76+
77+
expect(result).toContain(testDir1);
78+
expect(result).not.toContain(nonExistentDir);
79+
expect(result).not.toContain(testFile);
80+
expect(result).not.toContain(invalidPath);
81+
expect(result).toHaveLength(1);
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)