I recently built a real-time document server using Spring AI and the Model Context Protocol, and it completely changed how I think about giving AI access to documents.
TL;DR: If you have fewer than 100 documents that change frequently, skip the complexity of RAG and use MCP Resources instead. I'll show you exactly how.
π¬ Watch the Full Video Tutorial
Prefer reading? Keep scrolling! But the video includes live demos and troubleshooting tips.
Why I Stopped Defaulting to RAG
Like many developers, I used to reach for RAG (Retrieval Augmented Generation) for every document-based AI project:
- Vector database? β Check
- Embedding model? β Check
- Chunking strategy? β Check
- Hours of setup? β Ugh...
But then I hit a wall with a personal project: I just wanted AI to read 10 text files that I update daily.
Setting up Pinecone, generating embeddings, and dealing with re-indexing delays felt like using a sledgehammer to crack a nut.
That's when I discovered MCP Resources.
What is MCP? (In 60 Seconds)
Model Context Protocol (MCP) is an open standard by Anthropic that lets AI applications connect to data sources - think "USB ports for AI."
Instead of this (RAG):
Document β Chunk β Embed β Vector DB β Semantic Search β AI
You get this (MCP Resources):
Document β AI (that's it!)
The kicker? For my use case (personal documents, config files, live logs), MCP was 10x simpler.
What We're Building Today
In my video tutorial, I walk through building a production-ready MCP server that:
β
Exposes documents from any directory as MCP resources
β
Automatically detects file changes (add/modify/delete)
β
Notifies connected clients in real-time
β
Works with Spring AI out of the box
β
Takes less than 30 minutes to implement
The Code (Simplified)
Here's the core of what we build in the video:
1. Main Application
@SpringBootApplication
public class SpringAiMcpServerResourcesApplication {
@Value("${resource.directory.path}")
private String directoryPath;
public static void main(String[] args) {
SpringApplication.run(SpringAiMcpServerResourcesApplication.class, args);
}
@Bean
public List<McpServerFeatures.SyncResourceSpecification> myResources() {
String path = directoryPath;
Path folderPath = Paths.get(path);
try(Stream<Path> paths = Files.list(folderPath)) {
List<McpServerFeatures.SyncResourceSpecification> resources = paths
.map(this::getResourceSpecification)
.collect(Collectors.toList());
return resources;
} catch (IOException e) {
return Collections.emptyList();
}
}
private McpServerFeatures.SyncResourceSpecification getResourceSpecification(Path filePath) {
String fileName = filePath.getFileName().toString();
Path absolutePath = filePath.toAbsolutePath();
String fileUri = "file://" + absolutePath;
String mimeType = "text/plain";
var documentResource = new McpSchema.Resource(
fileUri,
fileName,
"Document: " + fileName,
mimeType,
null
);
McpServerFeatures.SyncResourceSpecification spec = new McpServerFeatures.SyncResourceSpecification(documentResource,
(exchange, request) -> {
try {
String textContent = Files.readString(absolutePath);
McpSchema.TextResourceContents textContents = new McpSchema.TextResourceContents(request.uri(), mimeType, textContent);
return new McpSchema.ReadResourceResult(List.of(textContents));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return spec;
}
}
What's happening:
- The
@Beanscans your documents directory - Creates a
ResourceSpecificationfor each file - Spring AI MCP automatically exposes them to clients
Use RAG when:
- You have 1000+ documents
- Documents are relatively static
- You need semantic search ("find similar concepts")
- Building a company knowledge base
Use MCP Resources when:
- You have 1-100 documents
- Documents change frequently
- You need real-time access
- Working with config files, logs, or personal docs
- You want simple infrastructure
Key Takeaways
β
RAG isn't always the answer - question your defaults
β
MCP Resources are simpler for small document sets
β
Real-time updates beat re-indexing delays
β
30-minute implementation vs hours of RAG setup
β
Perfect for personal tools and small teams
What's Next?
Part 2 (Coming Soon): Building the MCP Client
- Connecting to our server
- Using AI to analyze documents
- Automatic updates when files change
π Subscribe on YouTube so you don't miss it!
Resources
πΊ Full Video Tutorial - Watch the complete implementation
π» Source Code on GitHub - Clone and run locally
π MCP Specification - Protocol details
Follow me for more Spring AI tutorials! π
Top comments (0)