feat: improve roots messaging to distinguish client support vs configuration

- Add clientSupportsRoots tracking variable
- Set clientSupportsRoots during initialization based on client capabilities
- Update listRoots tool to provide clearer messaging:
  - Specific message when client doesn't support roots protocol
  - Different message when client supports roots but none are configured
- Improves user experience by clearly explaining the different scenarios

Addresses feedback from @olaservo in PR review
This commit is contained in:
AjayKumbham
2025-08-27 20:24:23 +05:30
parent cf9f66c14e
commit 39c1ca8df0

View File

@@ -180,6 +180,7 @@ export const createServer = () => {
// Roots state management // Roots state management
let currentRoots: Root[] = []; let currentRoots: Root[] = [];
let clientSupportsRoots = false;
const messages = [ const messages = [
{ level: "debug", data: "Debug-level message" }, { level: "debug", data: "Debug-level message" },
{ level: "info", data: "Info-level message" }, { level: "info", data: "Info-level message" },
@@ -841,15 +842,28 @@ export const createServer = () => {
if (name === ToolName.LIST_ROOTS) { if (name === ToolName.LIST_ROOTS) {
ListRootsSchema.parse(args); ListRootsSchema.parse(args);
if (!clientSupportsRoots) {
return {
content: [
{
type: "text",
text: "The MCP client does not support the roots protocol.\n\n" +
"This means the server cannot access information about the client's workspace directories or file system roots."
}
]
};
}
if (currentRoots.length === 0) { if (currentRoots.length === 0) {
return { return {
content: [ content: [
{ {
type: "text", type: "text",
text: "No roots are currently set. This could mean:\n" + text: "The client supports roots but no roots are currently configured.\n\n" +
"1. The client doesn't support the MCP roots protocol\n" + "This could mean:\n" +
"2. The client hasn't provided any roots yet\n" + "1. The client hasn't provided any roots yet\n" +
"3. The client provided empty roots" "2. The client provided an empty roots list\n" +
"3. The roots configuration is still being loaded"
} }
] ]
}; };
@@ -955,6 +969,7 @@ export const createServer = () => {
const clientCapabilities = server.getClientCapabilities(); const clientCapabilities = server.getClientCapabilities();
if (clientCapabilities?.roots) { if (clientCapabilities?.roots) {
clientSupportsRoots = true;
try { try {
const response = await server.listRoots(); const response = await server.listRoots();
if (response && 'roots' in response) { if (response && 'roots' in response) {