cd /news/ai-agents/from-api-first-to-mcp-designing-an-a… Β· home β€Ί topics β€Ί ai-agents β€Ί article
[ARTICLE Β· art-179437] src=dev.to β†— pub= topic=ai-agents verified=true sentiment=↑ positive

From API-First to MCP: Designing an AI-Connected Application

A developer built PEA Tracker, an open-source investment portfolio application that exposes its business logic through both REST and Model Context Protocol (MCP) interfaces, treating MCP as an adapter alongside REST in a hexagonal, API-first architecture. The project runs on Cloudflare Workers and D1 with a TypeScript/Hono backend, while Python and yfinance handle market data ingestion via GitHub Actions, keeping financial business rules independent of any AI assistant. The developer reports that building the MCP tools was only part of the challenge, with authentication, data collection, and production issues proving more complex than expected.

recommendation

Keep business logic in interface-agnostic application services and expose MCP as one adapter next to REST, so AI clients and future UIs share the same tested rules.

more in playbook: Designing agent systems
by read11 min views3 publishedDec 1, 2026

How hard could it be to connect an AI assistant to my investment portfolio?

What started as an experiment with MCP turned into PEA Tracker, a software project involving architecture, security, financial business rules, and a few unexpected production issues.

Here's what I learned along the way, both about designing AI-connected applications and about working with AI coding agents.

I had been using AI for some time to help me monitor and analyze my investment portfolio. But I still had to share screenshots from my broker regularly and update the data manually.

At the same time, I was exploring the Model Context Protocol (MCP). I'd read several articles about it, but I needed hands-on experience to understand what it could do and where its limits were.

So why not solve both problems by building my first MCP server?

It sounded simple enough.

At first, I thought I would just build a connector to give an AI assistant access to my financial data. My research and conversations with different AI assistants led me to reconsider that idea: I also needed to think about data collection, storage, and access controls.

I defined the requirements, broke the work into stages, and laid out a roadmap. What had started as an MCP experiment was becoming a full software project.

As it turns out, building the MCP tools was only part of the challenge. Authentication, data collection, financial business rules, and production issues made things more complicated than I had imagined.

My little MCP experiment was no longer quite so little.

Before building the MCP tools, I needed to decide where the portfolio management rules would live. I didn't want financial calculations to depend on an AI assistant, nor did I want to rewrite them if I eventually decided to build a web interface.

I was already familiar with API-first design and hexagonal architecture, having applied both on two or three projects. PEA Tracker was an opportunity to explore these approaches further in a different context: an AI-connected application.

I adopted both approaches. The REST and MCP interfaces rely on the same application services, while the business rules remain independent of the interfaces and the database. In this architecture, MCP is an adapter, just like REST.

There was also the question of budget. This was a personal project, and my preferred infrastructure budget was as close to zero as possible. Cloudflare Workers and D1 let me meet that requirement without managing a server.

I had considered Python and FastAPI for the backend. Integration constraints with Workers led me to TypeScript and Hono. I kept Python for collecting market quotes with yfinance, running through GitHub Actions. I saw no reason to force every component to use the same language.

I also separated market data ingestion from portfolio queries. They have different responsibilities and authorization requirements.

Figure 1 β€” REST and MCP share the same business logic, while market data follows a separate ingestion path.

These choices required more work upfront than building a simple MCP server. But when I moved from portfolio queries to recording financial transactions in V1.1, I could extend the business services without rethinking the role of the interfaces. The separation also let me test the financial rules independently of the AI client.

I didn't initially need a tool that could handle every transaction in my portfolio. I first wanted to check that an AI assistant could connect to my application and retrieve real data. I started with a proof of concept (PoC) using a simple ping tool to verify communication between the AI client and the MCP server before implementing business features.

V1.0 focused on read-only access: retrieving financial instruments, positions, available cash, and market quotes, then obtaining a portfolio valuation. I could ask questions without having to supply all that information again.

With V1.1, I expanded the scope to portfolio management. The application now needed to record transactions that had already taken place at my broker, in addition to reading data. That meant revisiting authorization and introducing an approval mechanism independent of the AI assistant.

Here's something I'd underestimated: designing tools for AI assistants.

Imagine having a perfectly functional API but an assistant that can't figure out which parameter to supply. That's exactly what happened with my portfolio identifier.

A read tool expected an identifier that the assistant couldn't reliably discover. Adding a tool to list portfolios solved the problem without me having to supply the identifier manually or relax access controls.

By V1.1.5, the server exposed 19 MCP tools. What mattered to me was whether those tools could work together to meet a real need.

While the application was read-only, the main security concern was protecting access to my financial data. With V1.1, I needed to let an AI assistant record a transaction while keeping the authority to approve it outside the assistant. The application was not placing stock market orders. It was recording transactions that had already taken place at my broker. Even so, an incorrect entry could distort the portfolio's state and performance calculations.

I separated transaction preparation from approval. The assistant can prepare a proposal based on the information I provide. I then open a link in my browser, authenticate, review the details, and approve the proposal. The assistant can then request confirmation, but the server checks that all the necessary conditions are met before recording the transaction.

The proposal is bound to the data being approved, expires after a limited period, and can only be used once to record the transaction. MCP and browser access also use distinct audiences. The assistant cannot replace that approval by claiming in the conversation that it has been granted.

Figure 2 β€” The assistant prepares the proposal and requests confirmation; only the server authorizes the write after independent approval by the portfolio owner.

This separation matters even if the assistant misinterprets an instruction or attempts to call a tool without sufficient authorization. The application remains responsible for enforcing authorization.

On paper, the workflow seemed straightforward. In production? Not quite.

Implementing it with OAuth 2.0 and Cloudflare's access controls exposed behaviors that my local tests had not revealed.

You know that satisfying moment when all your tests pass?

Well, production had other plans. πŸ˜…

Several times during the project, the application behaved differently in production, and the problem wasn't always in the code I had just written.

When setting up the market data collector, for example, a request made with curl succeeded while the Python script received an HTTP 403 response. Using certifi and adjusting the User-Agent got the script working, although the exact cause of the original rejection remained uncertain.

I also encountered problems with scheduled GitHub Actions runs. I eventually moved workflow triggering to Cloudflare Cron and added monitoring for collection cycles. Knowing that the script worked was no longer enough: I needed to know whether it had actually run.

The most instructive incident, however, involved MCP authentication and the financial approval workflow. The server received a JWT assertion containing two audiences (the intended recipients of the token), while my validator accepted only one. Diagnostics identified the exact reason for the rejection, but not why Cloudflare had issued that particular combination of audiences.

I adapted the MCP validator to explicitly accept that pair, while retaining the other checks and keeping an exclusive audience for browser-based approval.

A second issue arose when submitting the approval form in the browser. The Referrer-Policy: no-referrer policy caused the browser to send an Origin: null header. The server correctly rejected the request. Switching to strict-origin resolved the issue without disabling origin validation or CSRF protections.

These incidents taught me to distinguish more carefully between an application defect, an integration problem, and a legitimate security rejection. Relaxing security controls just to make an authentication workflow succeed would have been the wrong response.

The transaction-recording workflow was eventually validated in production using a real transaction. That did not mean every failure, concurrency, or replay scenario had been exercised in production. I want to keep that distinction clear in future projects.

I also wanted PEA Tracker to give me more experience with AI-assisted development and help me structure my work with coding agents. I was already using the AIDD (AI-Driven Development) framework, mainly for project memory and documentation. With PEA Tracker and Codex, I gradually extended its use to specifications, planning, feasibility studies, and quality checks. I also introduced project-specific technical skills as needs arose.

As the project progressed, I became more deliberate about preparing tasks: defining requirements, the scope of changes, and acceptance criteria before implementation began. This aligns with Spec-Driven Development. A vague request leaves the agent to make decisions I would rather make myself, particularly when architecture or security is involved.

I also had to manage context between sessions. The agent needs access to previous decisions, project constraints, and earlier test results. Project memory, AIDD documentation, technical history, and instructions helped me maintain that continuity. This gave me practical experience with one aspect of Context Engineering.

Asking an agent to produce code was not enough. I also needed to structure its working environment: instructions, available tools, tests, validation mechanisms, and restrictions on sensitive operations. Setting up these controls for agent behavior aligns with the principles of Harness Engineering.

Working with coding agents wasn't always smooth sailing either. Analyses were repeated, tests ran again without relevant changes, and context windows kept growing.

My token allowance certainly noticed.

All of this consumed time and resources without necessarily improving the result. I gradually learned to narrow task scopes, select relevant skills, and reuse existing evidence.

I remained responsible for decisions and approvals, particularly before deployments and financial operations. This human-in-the-loop approach was part of how I worked with the agents.

Figure 3 β€” AI-assisted development loop: context and evidence inform subsequent tasks, while sensitive decisions remain under human control.

I initially focused on structuring development tasks before exploring agent orchestration. Starting with V2.0, I've been experimenting with manual multi-agent orchestration. I work with an orchestrator agent to coordinate two sub-agents, using Git worktrees to isolate their tasks. I oversee their work and remain responsible for technical decisions. Exploring AIDD's orchestration module is a next step I'd like to take.

Would I make the same architectural choices if I started PEA Tracker again today? Yes.

Would I tackle everything in the same order? Definitely not.

I would start by investigating the most uncertain integration points: authentication with an actual MCP client, data provider limitations, and execution conditions in the cloud environment. A few focused proofs of concept would let me base decisions on observed results rather than assumptions about compatibility.

I would also introduce production diagnostics earlier. When access is denied, knowing which check failed, without exposing sensitive data, helps me avoid changing several components blindly. I would test complete workflows with the AI client, without supplying identifiers that it is supposed to discover on its own.

When working with coding agents, I would favor shorter tasks and define a validation strategy before starting them. I would reserve full test suites for integration milestones and rely on targeted tests during development. I'd also make more of an effort to preserve and reuse existing results, reducing repeated analysis and verification across sessions.

Finally, I would continue delivering usable versions incrementally. Using the application regularly with my own portfolio gives me feedback I wouldn't have gotten if I'd waited until every planned feature was implemented to start using it.

PEA Tracker took me from reading about MCP to designing an application that serves a personal need. It helped me better understand software architecture, AI assistant integration, and how I work with coding agents.

Since the versions covered in this article, the project has evolved into V2.0, which introduces the foundations for multiple portfolios and data providers. The next step will focus on strengthening MCP security, drawing especially on the OWASP MCP Top 10. I also want to spend more time exploring the agentic development practices I described here.

My main takeaway is that connecting an application to AI does not remove the need to design it as a proper piece of software. Business rules, authorization, data quality, and reliability remain the application's responsibilities.

I've published more detail about the technical decisions and incidents in an API-first, MCP and AI guide in the project's GitHub repository. For me, this experience is a first step toward designing other AI-connected applications.

Have you built an AI-connected application or integrated MCP into an existing architecture?

What caught you by surprise? Security? Tool design? Production behaving differently from your local environment?

I'd love to hear about your experience in the comments!

OpenAPI Initiative β€” REST API specifications and contracts. RFC 9700 β€” Best Current Practice for OAuth 2.0 Security β€” OAuth security best practices.

GitHub Spec Kit β€” Spec-Driven Development β€” an example of a specification-driven approach, included for further reading rather than as a tool used in PEA Tracker.

This article was written and reviewed by the author with the assistance of AI tools.

── more in #ai-agents 4 stories Β· sorted by recency
── more on @pea tracker 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain β€” perfect for shipping the agent you just read about.

$git push zahid main
β†’ Live at https://your-agent.zahid.host βœ“
Get free account β†’ Pricing
from €0/mo Β· no card required
LIVE [news/from-api-first-to-mc…] indexed:0 read:11min 2026-12-01 Β· β€”