r/opencode • • Sep 06 '26

Built a custom MCP server to reduce token usage

Post image

Hi

I am currently doing major in AI and built this project during my freetime. Its not perfectly polished and it definitely cannot replace Opencode but it is a good proof of concept which opencode can adopt to reduce token usage.

For more information look at the github repo: https://github.com/igiamronit/justbetter-mcp

You can use it by cloning the repo or by just running of npx command(npx install justbetter-mcp)

I would love if you guys can star the repo as it will help me keep the motivation and further improve on the project :)

If you want to know more technical aspect of the project. I have explained all of that in the README of github. You can check it out from there.

THANK YOU FOR READING IT TILL HERE

157 Upvotes

39 comments sorted by

19

u/lyphats Sep 06 '26

Have you noticed any drop in task quality after reducing token usage?

9

u/randomdick_18 Sep 06 '26

No drop in task quality. In theory the task quality should improve. Reason being we shift tool calling responsibility from llm to semantic search model. Hence llm can focus more on just executing the task

7

u/Usernamealready94 Sep 06 '26

How are you benchmarking it ? , just anectodal stuff or is there a benchmark ( doesnt need to be an official )

5

u/randomdick_18 Sep 06 '26

Task quality is anecdotal. Token usage is measured precisely

1

u/YogurtExternal7923 Sep 06 '26

How can a semantic search model do tool calls?

4

u/randomdick_18 Sep 06 '26

No semantic search model figures out what all tools llm will probably need. And then gives only schema of only those tools to llm

1

u/Efficient_Try_9136 Sep 06 '26

As I remember we used mcpo for loading on request and not bloating with all definitions

2

u/randomdick_18 Sep 06 '26

Claude code has recently starting doing this. Before that they actually used to bloat them with all defns.

In long tasks llm has to constantly switch its reasoning between "which tool to call" and "implementation details". If we shift that responsibility to semantic search model and use the above technique only as a backup when semantic search fails to return relevant tool, it will help llm to perform task better.

3

u/cbert33 Sep 06 '26

I was confused on the purpose of this until this comment. Now this makes much more sense. Very interesting, going to check it out!

1

u/YogurtExternal7923 Sep 06 '26

OH. I have that problem

4

u/tulwio Sep 06 '26

Any evals on task success too? Token savings look good but curious if tool selection/accuracy takes a hit vs baseline

1

u/randomdick_18 Sep 06 '26

I couldnt do extensive benchmarking because of token limitations(I need pure api credits, opencode free models wont work for mode 1).

But the few task i tested i manually verified them. They was no significant difference between output quality/task success.

If you see task success is theoretically lowerbounded by baseline cause it always has tool request as backup.

3

u/torrso Sep 07 '26

A couple more thoughts about this..

  1. OpenAI and Anthropic have something built in for this: https://developers.openai.com/api/docs/guides/tools-tool-search https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool - Codex And Claude utilize this natively, OpenCode does not, but there's some fork here: https://github.com/famitzsy8/opencode-tool-search-tool and I wouldn't be surprised if something like this ends up in OpenCode's core sooner or later if they see value in it and the Chinese models work well with it.

  2. What about tool discovery - how do the models know to search for a tool for something they didn't realize there is a tool for? OpenAI and Anthropic train their models around this behavior and recommend exposing tool categories plus a few common tools directly. With an arbitrary request_tools proxy, successful discovery seems much more dependent on prompting (which is extra tokens). Of course if your proxy can bring this recommended pattern to harnesses that don't do it internally, I guess it can work?

  3. How much money does this actually save? Total token count is not enough to answer that once prompt caching is involved.

As an example from my environment, approximately 9.4k tokens of a 65-tool catalog is parameter schemas. With cache hits, eliminating all 9.4k saves about $0.0094 per model request on GPT-6 Astra. On GPT-5.6 Luna, it saves about $0.000188 per request.

A single 200-token Astra discovery output already costs $0.01, cancelling the cached-input saving from removing 10k cached schema tokens on that request.

If discovery happens frequently, it could easily end up costing more than it saves?

1

u/randomdick_18 Sep 07 '26
  1. yes i am aware of the tool search released by anthropic and openai. The point of this project is that you can use that same feature with any ide(cursor, antigravity, vscode) just like you will use any other mcp server

  2. yes i am thinking of improving this in next version. I was thinking of maybe whenever a mcp server added a cheap llm request which summarizes what all types of tool llm has access to.

  3. I couldnt do rigorous testing about it due to token constraints

Thanks for your feedback!

1

u/TheOriginalOnee Sep 07 '26

Is there any OpenAI/Anthropic documentation regarding memory as well? It would be nice to have Claude’s memory system implemented in OpenCode too.

6

u/DominikPlays Sep 06 '26

If i can ask, how does an mcp server reduce tokens, is it like, when the ai is reading the codebase it optimizes that or how?

1

u/Interesting_Ad2316 Sep 10 '26 edited Sep 10 '26

it basically becomes a gateway between the mcps and the agent. Whenever you’re starting a session a huge schema for the mcps enabled is dumped into the context and the the model decides every turn if it wants to use something from that schema, i think their program prevents that schema being dumped into the context and makes the tool calling semantic happening outside of the session or at least by crunching the full mcp schema down to only the description text of the mcp

1

u/randomdick_18 Sep 06 '26

If you see any issues or have any doubt please lmk. Feel free to dm me also

5

u/verylittlegravitaas Sep 06 '26 • Bravo

Thank you so much. I’ve been looking for a good dynamic tool use option for awhile.

1

u/verylittlegravitaas Sep 06 '26

Do you have plans to implement proxy support for remote MCP servers?

1

u/randomdick_18 Sep 07 '26

Its already there

1

u/verylittlegravitaas Sep 07 '26

I mean HTTP streaming or SSE MCP servers. Unless I'm missing something I only see STDIO supported. https://github.com/igiamronit/justbetter-mcp/blob/main/src/upstream.ts#L111-L128

1

u/torrso Sep 07 '26

gpt-5.6-sol says:

There is a significant correctness hole: tool identity is only partially namespaced. Database IDs use server:tool, but discovery state, advertised schemas, deduplication, configuration, and invocation use bare tool names. When two upstreams expose the same name, it warns and routes alphabetically in src/catalog.ts:278. That can validate or advertise one server’s schema and invoke another server’s tool.

1

u/randomdick_18 Sep 07 '26

Yes when two server have same name, this bug can happen. But hey how many times you had two use two mcps with the same name

1

u/torrso Sep 07 '26

I have currently two mcps in use that have several tools with the same names.

1

u/randomdick_18 Sep 07 '26

Still i will fix it in next release

1

u/geearf Sep 07 '26

Why that instead of https://github.com/knowsuchagency/mcp2cli ? Have you compared the 2?

1

u/randomdick_18 Sep 07 '26

This is very different

1

u/pisa_p Sep 09 '26 edited Sep 09 '26

Hi — great project, exactly the pattern I was looking for. Two questions about scaling it to my setup (560 tools across ~19 LiteLLM-aggregated MCP groups, reached via HTTP, not local stdio processes): Your benchmark uses 4 upstream servers. Have you tested request_tools' semantic retrieval accuracy at a much larger scale (500+ tools, many near-duplicate domains like Gmail/Calendar/Drive)? Any known degradation in top-k relevance at that size?

2

u/randomdick_18 Sep 10 '26

no i am yet to do large proper benchmarking. its true that at larger scale retrieval quality will decrease. i am currently thinking of any cheap and smart solution for it

1

u/pisa_p Sep 11 '26

I ended today a proxy MCP for my problem, with 560 tools works quite well. Not semantic, I keep the idea from Hermes agent tool_search. Now I have to test for some days...

1

u/Fluidetom Sep 12 '26

Isn’t that the same as sleev.ai ?

1

u/randomdick_18 Sep 14 '26

hey i dont wanna be rude or anything but please read the post or the readme before commenting. they are not even similar

1

u/t12e_ Sep 06 '26

Seems cool. How does it compare to using skills with scripts/cli