Using Resources
Resources allow the server to expose data (files, database records, logs) to the client. Resources can be static (fixed URI) or dynamic (URI templates).
Listing Resources
Available resources are listed in the resources and resourceTemplates arrays returned by the hook.
const { resources, resourceTemplates } = useMcp({ url: '...' });
Reading a Resource
To get the content of a specific resource, use readResource. This works for both static resources and concrete URIs derived from templates.
const { readResource } = useMcp({ url: '...' });
const loadFile = async () => {
try {
// Returns an object with a 'contents' array
const result = await readResource('file://docs/readme.md');
result.contents.forEach(item => {
console.log(`MIME: ${item.mimeType}`);
console.log(`Content: ${item.text || item.blob}`);
});
} catch (error) {
console.error('Failed to read resource:', error);
}
};
Refreshing Lists
Resources on the server may change. You can manually trigger a refresh of the resource list:
const { listResources } = useMcp({ url: '...' });
// Call this to update the local 'resources' state
await listResources();